We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.



43.9k
Accepted
answer

Hi,

you may set different database connection as explained in documentation: https://docs.phalcon.io/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: https://docs.phalcon.io/en/latest/reference/models.html#setting-multiple-databases