Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to join tables and find by conditions in Cakephp 2 ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 117
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: