php - Better way to retrieve HABTM, HasMany...etc data than -1 recursive manual joins in CakePHP? -
i have database of events. created model-function retrieve events based on city, type, subtype, start & end dates.
it's working (mostly), geez, it's novel worth of code. there better way this? (it's lot of code, not hard follow i'm doing):
some of associations:
event belongsto restaurant event belongsto venue restaurant belongsto city venue belongsto city event hasandbelongstomany eventtype event hasandbelongstomany eventsubtype event hasmany schedule schedule hasmany date (and of course belongsto/hasmany counterparts set too)
my "getevents" function (in event model):
function getevents($opts) { //$opts = limit, start, end, fields, types, subtypes, subsubtypes, cities //dates $qopts['start'] = date('y-m-d') . ' 00:00:00'; if(isset($opts['start'])) $qopts['start'] = $opts['start']; $qopts['end'] = date('y-m-d') . ' 23:59:59'; if(isset($opts['end'])) $qopts['end'] = $opts['end']; //limit if(isset($opts['limit'])) $qopts['limit'] = $opts['limit']; //fields $qopts['fields'] = array('event.id', 'event.name', 'event.slug', 'city.name', 'date.start'); if(isset($opts['fields'])) $qopts['fields'] = $opts['fields']; //joins $qopts['joins'] = array( array('table' => 'schedules', 'alias' => 'schedule', 'type' => 'left', 'conditions' => array( 'event.id = schedule.event_id', ) ), array('table' => 'dates', 'alias' => 'date', 'type' => 'left', 'order' => 'date.start asc', 'conditions' => array( 'date.schedule_id = schedule.id', ), ), array('table' => 'venues', 'alias' => 'venue', 'type' => 'left', 'conditions' => array( 'event.venue_id = venue.id', ) ), array('table' => 'restaurants', 'alias' => 'restaurant', 'type' => 'left', 'conditions' => array( 'event.restaurant_id = restaurant.id', ) ), array('table' => 'cities', 'alias' => 'city', 'type' => 'left', 'conditions' => array( 'or' => array( 'venue.city_id = city.id', 'restaurant.city_id = city.id', ), ) ), array('table' => 'event_types_events', 'alias' => 'eventtypesevent', 'type' => 'left', 'conditions' => array( 'eventtypesevent.event_id = event.id', ) ), array('table' => 'event_sub_types_events', 'alias' => 'eventsubtypesevent', 'type' => 'left', 'conditions' => array( 'eventsubtypesevent.event_id = event.id', ) ), array('table' => 'event_types', 'alias' => 'eventtype', 'type' => 'left', 'conditions' => array( 'eventtypesevent.event_type_id = eventtype.id', ) ), array('table' => 'event_sub_types', 'alias' => 'eventsubtype', 'type' => 'left', 'conditions' => array( 'eventsubtypesevent.event_sub_type_id = eventsubtype.id', ) ), ); //date conditions $qopts['conditions'] = array( "date.start >=" => $qopts['start'], "date.start <=" => $qopts['end'], ); //cities conditions if(isset($opts['cities'])) { if(is_array($opts['cities'])) { $cityconditions['or'] = array(); foreach($opts['cities'] $city_id) { array_push($cityconditions['or'], array('city.id'=>$city_id)); } array_push($qopts['conditions'], $cityconditions); } } //event types conditions if(isset($opts['event_types'])) { if(is_array($opts['event_types'])) { $eventtypeconditions['or'] = array(); foreach($opts['event_types'] $event_type_id) { array_push($eventtypeconditions['or'], array('eventtype.id'=>$event_type_id)); } array_push($qopts['conditions'], $eventtypeconditions); } } //event sub types conditions if(isset($opts['event_sub_types'])) { if(is_array($opts['event_sub_types'])) { $eventsubtypeconditions['or'] = array(); foreach($opts['event_sub_types'] $event_sub_type_id) { array_push($eventsubtypeconditions['or'], array('eventsubtype.id'=>$event_sub_type_id)); } array_push($qopts['conditions'], $eventsubtypeconditions); } } $this->recursive = -1; $data = $this->find('all', $qopts); return $data; }
bonus points: have question on stackoverflow (here) asks function - trying figure out how return multiple dates instead of 1 per event.
dave moron, set associations , normal (recursive) find()
. if want more control on associated data that's retrieved, use containablebehavior.
if have conditions on associated model requires join other tables primary query, that's different topic , there many ways go (search or ask specific question). purpose manual joins may appropriate. not need manually join tables retrieve associated data though.
Comments
Post a Comment