Search code examples
phplithium

Different database for some models in lithium


My app requires me to use 2 databases. Most models use data from only one model but some models require me to use a different database. Is it possible to specify in a model which database to use? I am using MongoDB for my database.


Solution

  • The Lithium docs explain multiple connections, in the section "Model Creation and Configuration"

    From that page, if you want to use the "backup" connection:

    <?php
    namespace app\models;
    class Posts extends \lithium\data\Model {
        protected $_meta = array(
            'connection' => 'backup'
        );
    }
    ?>
    

    Further explanation from the same page:

    Once your model's $_meta property has been configured, Lithium merges it with the default settings at runtime. Because the Post model has a specified connection, the backup connection is used instead of the default.