php - CakePHP - Find hasMany association without hasMany relation in Model? -
again want ask question cakephp : have 2 model user , comment relation 1-n (one user have many comment). want use find() list infomation both user , it's comments format data return :
array ( [user] => array ( [id] => 121 [name] => gwoo kungwoo [created] => 2007-05-01 10:31:01 ) [comment] => array ( [0] => array ( [id] => 123 [user_id] => 121 [title] => on gwoo kungwoo [body] => kungwooness not gwooish [created] => 2006-05-01 10:31:01 ) [1] => array ( [id] => 124 [user_id] => 121 [title] => more on gwoo [body] => of 'nut? [created] => 2006-05-01 10:41:01 ) ) )
but don't want config relationship hasmany in model user example in link : http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html. tried code :
$data = $this->user->find('all', array( 'joins' => array( array( 'table' => 'comment', 'alias' => 'comment', 'type' => 'left', 'conditions' => array( 'user.id = comment.user_id' ) ), 'conditions' => array( 'user.id' => $userid ), 'fields' => array('user.*', 'comment.*') ));
and cake return array duplicate record of user (ex : if user has 2 comments, returns 2 records of user duplicate).
anyone can give me solution ? ( except solution config hasmany relation model), thanks.
alternatively can use bindmodel in controller
$this->user->bindmodel( array( 'hasmany'=>array( 'comment' =>array( 'classname' => 'comment', 'foreignkey' => 'user_id', 'conditions' => array('comment.status' => 'active'), ) ) ) ); $data = $this->user->find('all',array('conditions'=>array('user.email'=>$useremail)));
for more info
updated
have @ answer info, see details bind , unbind model through link
Comments
Post a Comment