How to join tables and find by conditions in Cakephp 2 ?
There are 3 tables Venues, Venues type and Venue Opening Days. First and the most preferred way is use associations in the Model like hasMany, BelongsTo. Second way to join tables is to use the "joins" as shown below:
$this->Venue->find("all", array(
"joins" => array(
array(
"table" => "venue_venue_types",
"alias" => "VenueVenueType",
"type" => "LEFT",
"conditions" => array(
"Venue.id = VenueVenueType.venue_id"
)
),
array(
"table" => "venue_openingdays",
"alias" => "VenueOpeningday",
"type" => "LEFT",
"conditions" => array(
"Venue.id = VenueOpeningday.venue_id"
)
)
),
'conditions' => array(
'Venue.country_id' => $conditions['Venue.country_id'],
'Venue.city_id'=>$conditions['Venue.city_id'],
'VenueVenueType.venue_type_id'=>$conditions['VenueVenueType.venue_type_id'],
'Venue.allweekdays'=>$conditions['Venue.allweekdays']
)
));
Thats all... now your tables are joined and you can see the desired output.
Thanks for reading the blog.
0 Comment(s)