Reputation: 499
I am working on cake php and I have an association with users and user_details
table, When I sign up then its working fine. Its post data on both the tables but when I redirect to profile page then it throws an error of userDetail
array. When I printed that info array I got only one array instead of two arrays one is User and another one is UserDetail
. Its working fine on my local machine but not working on server, But when I used recursive=>1
than its works. Please let me know what wrong here.
Query
$userInfo = $this->User->find('first',array('conditions'=>array('User.id'=>$userId)));
when I pass recursive than it works
$userInfo = $this->User->find('first',array('recursive'=>1,'conditions'=>array('User.id'=>$userId)));
Thanks
Shiv
Upvotes: 0
Views: 65
Reputation: 1595
Please use Containable
behavior. It's more intuitive and clean then recursive
variable. You can do this adding in your AppModel
(or specific models if you don't want this behavior in all of them):
public $actsAs = array('Containable');
Then, in your find:
$userInfo = $this->User->find('first',array(
'conditions'=>array(
'User.id'=> $userId
),
'contain' => array('UserDetail')
));
If you have declared your association, you will retrieve your User
along any UserDetail
for that User
Upvotes: 1
Reputation: 612
If you always want UserDetail to be fetched when you do a find on User, add the $recursive property to your User model:
public $recursive = 1;
However, the above is not recommended as usually, $recursive
should be set to -1 in your AppModel. This is because Model::find()
should normally only fetch data from a single model and not ALL related models. E.g. if your User model is related to say UserDetail and UserRole model, data from all of them will be fetched if $recursive is not equal to -1. This slows down your app.
When you do require related model data like in your question, use the containable behavior like this:
$userInfo = $this->User->find('first', array(
'conditions' => array('User.id'=>$userId),
'contain' => array('UserDetail')
));
Upvotes: 0