CakePHP finds a query in multiple data sources

I am trying to do the following:

  • My Company models are stored in the default data source. In model, I set var $ useDbConfig = 'default';
  • My "User" models are stored in another data source called 'Users_db. In this model, I set var $ useDbConfig = 'users_db';

There is an “owned” relationship between the Company and User models.

I want to make a recursive search query that returns all users for each company at a time. I have a feeling that I can’t do it with Cake, as he is now.

When I try the following:

$res2 = $this->Company->find('all', array(
                'conditions' => array('Company.id' => 1),
                'recursive'=>2
            ));

I get a message that he cannot find the users table (its search in the default source): Error: User_groups table for the model The user was not found in the datasource by default .

Can I do this?

Thanks so much for any help you can provide .....

kSeudo.

+5
source share
2 answers

Why not replace the original statement as follows:

$this->loadModel('User');
$res2 = $this->User->find('all', 
                array( 'conditions' => array( 'User.company_id' => 1 ) ));

This will lead to the desired results.

Otherwise, if you need something more accurate, you can add a function fetch_users()to the class Companythat implemented the same logic.

0
source

Ok, the problem is resolved. Once I set the data source in the model correctly, everything works fine.

Thanks guys,

0
source

All Articles