cakephp - How to get associated data of a model using custom join cakephp2 -
i using cakephp2 , trying join 2 tables :
$employee=$this -> employee ->find ('all',array( 'joins'=>array( array('table' => 'employee_histories', 'alias' => 'eh', 'type' => 'left', 'foreignkey' => false, 'conditions' => array('eh.emp_id= employee.emp_id')) ), 'fields'=>'employee.*,eh.*', ));
in employeehistory model there association table employee_designations :
public $belongsto = array( 'employeedesignation' => array( 'classname' => 'employeedesignation', 'foreignkey' => '', 'conditions' => 'employeedesignation.designation_id=employeehistory.designation_id', 'fields' => '', 'order' => '' ), );
now possible employeedesignation result in $employee array.
you can join employee_designations
table adding array joins
array. this:
$employee= $this->employee->find('all', array( 'joins'=>array( array( 'table' => 'employee_histories', 'alias' => 'eh', 'type' => 'left', 'foreignkey' => false, 'conditions' => array( 'eh.emp_id= employee.emp_id' ) ), // array joining employee designations table array( 'table' => 'employee_designations', 'alias' => 'ed', 'type' => 'left', 'conditions' => array( 'eh.designation_id = ed.designation_id' ) ) ), 'fields'=>'employee.*, eh.*, ed.*', // include ed fields ));
Comments
Post a Comment