php - Working out associations in CakePHP -
i trying larn cakephp, , attempting replicate can standard php , mysql cake.
i have event
table event information, , list_items
table holds entries bullet pointed list displayed on each event page. have venue
table lists venues (id, event_id , name) , instance
table joins event , venue - consists of id, event_id, venue_id, , date.
i have associated tables how think should be:
class event extends appmodel{ public $hasmany = array( 'listitem', 'instance' ); }
bullet list items:
class listitem extends appmodel{ public $belongsto = array( 'event' ); }
instances:
class instance extends appmodel{ public $belongsto = array( 'event' ); public $hasone = array( 'venue' ); }
and venue:
class venue extends appmodel{ public $belongsto = array( 'instance' );
and event controller has query find event based on id:
public function view($id = null){ if (!$id){ throw new notfoundexception(__('invalid course')); } $event = $this->event->findbyid($id); if (!$event){ throw new notfoundexception(__('invalid event')); } $this->set('event',$event); }
are these associations correct? want able display instance date alongside venue - @ moment can output date no problem, venue eludes me.
probably it's because recursiveness not deep enough. alter that, can following:
the easiest poorest way, alter recursiveness before find:
$this->event->recursive = 2; //change according deepness want
this retrieve relationships according deepness assigned, can't exclude relations not need.
or, more advanced richer way, add together containable
behavior , create recursiveness yourself. @ origin of model add together next behavior:
public $actsas = array('containable');
and in find function:
$event = $this->event->find('first', array( 'conditions' => array('id' => $id), 'contain' => array( 'listitem', 'instance' => array( 'venue' ) ) ));
the advantage of contain
on recursive
can command every aspect of relationships on find
queries, can retrieve info need. it's highly recommended utilize containable
behavior
please see documentation farther info: http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html
php mysql cakephp
No comments:
Post a Comment