php - CakePHP Threaded "Find" with Associated Models -
i using $this->find('threaded') in cakephp application , trying pull in associated model within find (habtm). have tried several methods, such 'joins', 'recursive' , 'contains' no luck. using cakephp 2.3.6
here (working) code.
class eventscontroller extends appcontroller { public function promoters($id = null) { $options = array('conditions' => array('event.id' => $id)); $event = $this->event->find('first', $options); $this->set('event', $event); $this->loadmodel('eventspromoter'); $treelistconditions = array( 'conditions' => array( 'event_id' => $id ), ); $promotertree = $this->eventspromoter->find('threaded', $treelistconditions); $this->set('promoters', $promotertree); } }
this results in following array output
array ( [0] => array ( [eventspromoter] => array ( [id] => 1 [promoter_id] => 1 [event_id] => 1 [parent_id] => [created] => 2013-07-26 00:30:09 [modified] => 2013-07-26 00:30:09 ) [children] => array ( [0] => array ( [eventspromoter] => array ( [id] => 10 [promoter_id] => 4 [event_id] => 1 [parent_id] => 1 [created] => 0000-00-00 00:00:00 [modified] => 0000-00-00 00:00:00 ) [children] => array ( ) ) [1] => array ( [eventspromoter] => array ( [id] => 13 [promoter_id] => 6 [event_id] => 1 [parent_id] => 1 [created] => 0000-00-00 00:00:00 [modified] => 0000-00-00 00:00:00 ) [children] => array ( ) ) ) ) [1] => array ( [eventspromoter] => array ( [id] => 2 [promoter_id] => 2 [event_id] => 1 [parent_id] => [created] => 2013-07-26 00:30:09 [modified] => 2013-07-26 00:30:09 ) [children] => array ( [0] => array ( [eventspromoter] => array ( [id] => 11 [promoter_id] => 5 [event_id] => 1 [parent_id] => 2 [created] => 0000-00-00 00:00:00 [modified] => 0000-00-00 00:00:00 ) [children] => array ( [0] => array ( [eventspromoter] => array ( [id] => 6 [promoter_id] => 3 [event_id] => 1 [parent_id] => 11 [created] => 0000-00-00 00:00:00 [modified] => 0000-00-00 00:00:00 ) [children] => array ( ) ) ) ) [1] => array ( [eventspromoter] => array ( [id] => 14 [promoter_id] => 7 [event_id] => 1 [parent_id] => 2 [created] => 2013-07-26 00:30:09 [modified] => 2013-07-26 00:30:09 ) [children] => array ( ) ) ) ) )
what code output following
array ( [0] => array ( [eventspromoter] => array ( [id] => 1 [promoter_id] => 1 [event_id] => 1 [parent_id] => [created] => 2013-07-26 00:30:09 [modified] => 2013-07-26 00:30:09 ) [promoter] => array ( [promoter_id] => 1 [first_name] => 'bob' [last_name] => 'smith' ) [children] => array ( [0] => array ( [eventspromoter] => array ( [id] => 10 [promoter_id] => 4 [event_id] => 1 [parent_id] => 1 [created] => 0000-00-00 00:00:00 [modified] => 0000-00-00 00:00:00 ) [promoter] => array ( [promoter_id] => 4 [first_name] => 'sally' [last_name] => 'sue' ) [children] => array ( ) ) [1] => array ( [eventspromoter] => array ( [id] => 13 [promoter_id] => 6 [event_id] => 1 [parent_id] => 1 [created] => 0000-00-00 00:00:00 [modified] => 0000-00-00 00:00:00 ) [promoter] => array ( [promoter_id] => 6 [first_name] => 'ben' [last_name] => 'king' ) [children] => array ( ) ) ) ) [1] => array ( [eventspromoter] => array ( [id] => 2 [promoter_id] => 2 [event_id] => 1 [parent_id] => [created] => 2013-07-26 00:30:09 [modified] => 2013-07-26 00:30:09 ) [promoter] => array ( [promoter_id] => 2 [first_name] => 'jack' [last_name] => 'sparrow' ) [children] => array ( [0] => array ( [eventspromoter] => array ( [id] => 11 [promoter_id] => 5 [event_id] => 1 [parent_id] => 2 [created] => 0000-00-00 00:00:00 [modified] => 0000-00-00 00:00:00 ) [promoter] => array ( [promoter_id] => 5 [first_name] => 'jane' [last_name] => 'doe' ) [children] => array ( [0] => array ( [eventspromoter] => array ( [id] => 6 [promoter_id] => 3 [event_id] => 1 [parent_id] => 11 [created] => 0000-00-00 00:00:00 [modified] => 0000-00-00 00:00:00 ) [promoter] => array ( [promoter_id] => 3 [first_name] => 'mike' [last_name] => 'jones' ) [children] => array ( ) ) ) ) [1] => array ( [eventspromoter] => array ( [id] => 14 [promoter_id] => 7 [event_id] => 1 [parent_id] => 2 [created] => 2013-07-26 00:30:09 [modified] => 2013-07-26 00:30:09 ) [promoter] => array ( [promoter_id] => 7 [first_name] => 'spider' [last_name] => 'man' ) [children] => array ( ) ) ) ) )
nothing have been trying working, , seems cake should able handle easily. in advance help!
i taking wrong approach trying accomplish. while still think should possible eventscontroller class, elected make in eventspromoterscontroller habtm class. code simpler using method
class eventspromoterscontroller extends appcontroller { public function event($event_id = null) { $options = array('conditions' => array('event_id' => $event_id)); $promoters = $this->eventspromoter->find('threaded', $options); $this->set('promoters', $promoters); } }
Comments
Post a Comment