Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

model with the same connection but different database setting

The code:

class Robots extends \Phalcon\Mvc\Model
{

    public function getSource()
    {
        return "dogs";
    }

}
$robot = Robots::findFirst(3);

The sqlStatement trac is: SELECT dogs.id, dogs.name, dogs.age, dogs.created_at, dogs.updated_at FROM dogs WHERE dogs.id = 3 LIMIT 1

I want the sqlStatement can be like this: SELECT dogs.id, dogs.name, dogs.age, dogs.created_at, dogs.updated_at FROM database.dogs WHERE dogs.id = 3 LIMIT 1

Just like the sqlStatement I want FROM dogs can be like FROM database.dogs

The model class can set the table name with the function getSource(), how can i set the database attribute with something when i want to use the same connection but the different database.



31.2k
Accepted
answer

Hi,

you may set different database connection as explained in documentation: http://docs.phalconphp.com/en/latest/reference/models.html#setting-multiple-databases

Thanks! I had read the page you are showing me the page before the question.But i can not found what i want.

I want sqlStatement can be like this:

SELECT a.id,b.title,b.title FROM db1.users a LEFT JOIN db2.posts b ON a.id = b.user_id

Hi,

you may set different database connection as explained in documentation: http://docs.phalconphp.com/en/latest/reference/models.html#setting-multiple-databases