Question:-
put conditions with hasMany model with find function and others
Answer:-
public $hasMany = array(
'Detail' => array(
'className' => 'Detail',
'foreignKey' => 'user_id',
'dependent' => true,
'conditions' => '',
'order' => 'created DESC',
),
'Category' => array(
'className' => 'Category',
'foreignKey' => 'user_id',
'dependent' => true,
'conditions' => '',
'order' => 'created DESC',
),
'Favorite' => array(
'className' => 'Favorite',
'foreignKey' => 'user_id',
'dependent' => true,
'conditions' => '',
'order' => 'created DESC',
)
);
----------------------------------
<?php
class User extends AppModel {
public $actsAs = array('Containable');
}
$users = $this->User->find('all', array(
'contain'=>array(
'UserDetail',
'Category'=>array(
'conditions'=>array('Category.user_category_id'=>1)
),
'Favorite'=>array(
'conditions'=>array('Favorite.member_id'=>8)
)
)
);
$this->set('test', $users);
By Joins
$users = $this->Item->find('all', array(
'joins' => array(
array(
'table' => 'categories',
'alias' => 'Category',
'type' => 'inner',
'conditions'=> array('Category.user_category_id' => 1)
),
array(
'table' => 'favorites',
'alias' => 'Favorite',
'type' => 'inner',
'conditions'=> array('Favorite.member_id' => 8, 'User.id = Favorite.user_id')
)
),
'contain'=>array('ItemDetail','Category','Favorite')
);
$this->set('test', $users);
Another Method
$this->Item->hasMany['Favorite']['conditions']['member_id'] = 8;
rollback to the previous behavior, you need to unset the condition:
unset($this->Item->hasMany['Favorite']['conditions']['member_id']);
------------------------------------------------------------------------
$this->bindModel(array(
'hasMany' => array(
'Comment' => array(
'conditions' => array('Comment.comment_type_id' => 123)
))));
$this->find('all');
Model is already binded with comment use :
$this->hasMany['Comment']['conditions'] = array('Comment.comment_type_id' => 123);
$this->find('all');
Anil Bist
Skills Cakephp
Qualifications :- High School - SLV, College/University - Graphic Era Deemed Univ University,Location :-Dehradun,Dehradun,Uttarakhand,
Description:-
I started my Professional Journey in 2006 with one of the Web Development Company in Bangalore and my 1st framework was "Ruby on Rail" as Web development and delivered around 5+ Projects using this platform. Then came another dimension as JEE/Sturst framework, Gradually I realized that I want to build something on my own and give my passion and energy on creating something different a
Explore
Students (5)
Ask
Rohin
Ask
Sanjay
Ask
Riya
Ask
deepu
Ask
divya
Recommended Classes
Anil Bist
I started my Professional Journey in 2006 with one of the Web Development Company in Bangalore an...
Professional
shweta Pandey
Passionate about LIFE and Learning. Lets explore together better version of "self", &qu...
Professional
shweta Pandey
Passionate about LIFE and Learning. Lets explore together better version of "self", &qu...
Professional