Wednesday, 15 February 2012

CakePHP HABTM relationship table joins -



CakePHP HABTM relationship table joins -

i have 'patient_cases' table habtm relationship 'procedures' table. 'procedures' tables has many 1 relationship 'consultants' table, has many 1 relationship 'specialties' table.

the relationships working fine , using cakephp debug tool bar can see coming through correctly when @ 'patient_cases' index page, i'm getting consultant_id , specialty_id rather of fields associated model.

which should happening believe -

in cakephp associations (belongsto , hasone) performs automatic joins retrieve data, can issue queries retrieve models based on info in related one.

but not case hasmany , hasandbelongstomany associations.

i want able consultant name 'consultants' table using procedures.consultant_id , specialty name 'specialties' using consultant.specialty_id.

i have tried playing table joins no success,

class patientcase extends appmodel { public $hasandbelongstomany = array( 'procedure' => array( 'classname' => 'procedure', 'jointable' => 'patient_cases_procedures', 'foreignkey' => 'patient_case_id', 'associationforeignkey' => 'procedure_id', 'unique' => true ) ); } class procedure extends appmodel { public $belongsto = array( 'consultant' => array( 'classname' => 'consultant' ) ); } class consultant extends appmodel { public $belongsto = array( 'specialty' => array( 'classname' => 'specialty', 'conditions' => array('specialty.active' => 1) ) ); public $hasmany = array( 'procedure' ); } class specialty extends appmodel { public $hasone = array( 'consultant' ); ); } $this->set('patientcases', $this->patientcase->find('all'));

returns need, each model array returned (other relationships not mentioned here), model array 'consultant' not returned, means can not 'specialty' info linked 'consultant' model

edit have managed info need using in patientcasescontroller

$this->set('patientcases', $this->patientcase->find('all')); $this->set('patientcaseprocedures', $this->patientcase->procedure->find('all'));

however, ideally want homecoming in 1 'patientcases', possible?

edit have solved problem using recursive function

$this->patientcase->recursive = 3; $this->set('patientcases', $this->patientcase->find('all'));

cakephp

No comments:

Post a Comment