Binding multiple models Cakephp

2019-02-20 14:21发布

I am trying to bind 3 models in cakephp.The relation is as follows

Member hasMany Member_Organaization Member_Organisations belongs to Organaization

i try to use

$this->Member->find('all',conditions)

it just show me only data upto hasMany association. I understand that the Member model is not related directly to the organization one. but how can we do it? My code is as follows:

$this->Member->bindModel(
               array(
                 'hasMany'=>array(
                     'NpoMember' =>array(
                      'className' => 'NpoMember',
                      'foreignKey' => 'member_id',
                      'conditions' => array('NpoMember.status' => 'Active'),
                  )         
               )
            )
        ); 
        $this->NpoMember->bindModel(
               array(
                 'belongsTo'=>array(
                     'Npo'=>array(
                      'className' => 'Npo',
                      'foreignKey' => 'npo_id',
                      'conditions' => array('Npo.status' => 'Active')
                    )        
               )
            )
        ); 
        $userData  = $this->Member->find('first',array('conditions'=>array('Member.email'=>$userEmail,'Member.password'=>$passWord,'Member.status'=>'Active')));

I found this site to be very helpful. Thanks and Regards Himanshu Sharma

1条回答
做个烂人
2楼-- · 2019-02-20 14:45

Use recursive the cakephp functionality for this type of purpose.

In your controller: $this->Member->recursive = 2; use this before your find query.

Refrence : http://book.cakephp.org/view/1063/recursive

查看更多
登录 后发表回答