php - Checking if selected rows are favourite by user in cakephp -
i have venue
table 20+ columns. have favorite_venues
table columns
id | userid | venueid
here code venues
$this->venue->virtualfields = array( 'bookedcount' => "select count(*) bookings venueid = venue.id" ); $result = $this->venue->find('all', array('conditions'=>$conditions, 'order' => array('venue.bookedcount desc'), 'limit' => 20, 'offset' => $offset * 20 ));
i want add condition if send userid, should check every venue if added favourite list or not , set
$venue['isfavorite'] = yes/no
i dont want loop , check every venue get. there way can incorporate in same mysql query in cakephp. not sure how put yes/no virtual field
you can left
join in favorite_venues
table, , example use case
statement in virtual field checks whether there linked row.
here's untested example illustrate mean. don't know how user id involved, got figure on own.
$this->venue->virtualfields = array( 'bookedcount' => "select count(*) bookings venueid = venue.id", 'isfavorite' => 'case when favoritevenues.id not null "yes" else "no" end' ); $result = $this->venue->find('all', array( 'conditions' => $conditions, 'order' => array('venue.bookedcount desc'), 'limit' => 20, 'offset' => $offset * 20, 'joins' => array( array( 'table' => 'favorite_venues', 'alias' => 'favoritevenues', 'type' => 'left', 'conditions' => array( 'favoritevenues.venueid = venue.id', ) ) ), // don't forget group prevent duplicate results 'group' => 'venue.id' ));
see also
Comments
Post a Comment