Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8536421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:39:35+00:00 2026-06-11T10:39:35+00:00

I use CakePHP 2.2.2 I have 3 tables: restaurants, kitchens and kitchens_restaurants – join

  • 0

I use CakePHP 2.2.2
I have 3 tables: restaurants, kitchens and kitchens_restaurants – join table for HABTM.

In Restaurant model I have:

public $hasAndBelongsToMany = array(
    'Kitchen' =>
        array(
            'className'              => 'Kitchen',
            'joinTable'              => 'kitchens_restaurants',
            'foreignKey'             => 'restaurant_id',
            'associationForeignKey'  => 'kitchen_id',
            'unique'                 => true,
            'conditions'             => '',
            'fields'                 => 'kitchen',
            'order'                  => '',
            'limit'                  => '',
            'offset'                 => '',
        ),

The problem is that I have separate controller for my main page in which I need to retrieve data from this models with complex conditions.

I added

public $uses = array('Restaurant');

to my main page controller and here comes the part where I need your advices.

I need to select only those restaurants where kitchen = $id.
I’ve tried to add

public function index() {   
$this->set('rests', $this->Restaurant->find('all', array(
'conditions' => array('Restaurant.active' => "1", 'Kitchen.id' => "1")
)));

}

and I got SQLSTATE[42S22]: Column not found: 1054 Unknown column in ‘where clause’ error.
Obviously I need to fetch data from “HABTM join table” but I don’t know how.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:39:37+00:00Added an answer on June 11, 2026 at 10:39 am

    TLDR:

    To retrieve data that’s limited based on conditions against a [ HABTM ]‘s association, you need to use [ Joins ].

    Explanation:

    The code below follows the [ Fat Model/Skinny Controller ] mantra, so the logic is mostly all in the model, and just gets called from a controller.

    Note: You don’t need all those HABTM parameters if you follow the [ CakePHP conventions ] (which it appears you are).

    The below code has not been tested (I wrote it on this site), but it should be pretty darn close and at least get you in the right direction.

    Code:

    //Restaurant model

    public $hasAndBelongsToMany = array('Kitchen');
    
    /**
     * Returns an array of restaurants based on a kitchen id
     * @param string $kitchenId - the id of a kitchen
     * @return array of restaurants
     */
    public function getRestaurantsByKitchenId($kitchenId = null) {
        if(empty($kitchenId)) return false;
        $restaurants = $this->find('all', array(
            'joins' => array(
                 array('table' => 'kitchens_restaurants',
                    'alias' => 'KitchensRestaurant',
                    'type' => 'INNER',
                    'conditions' => array(
                        'KitchensRestaurant.kitchen_id' => $kitchenId,
                        'KitchensRestaurant.restaurant_id = Restaurant.id'
                    )
                )
            ),
            'group' => 'Restaurant.id'
        ));
        return $restaurants;
    }
    

    //Any Controller

    public function whateverAction($kitchenId) {
        $this->loadModel('Restaurant'); //don't need this line if in RestaurantsController
        $restaurants = $this->Restaurant->getRestaurantsByKitchenId($kitchenId);
        $this->set('restaurants', $restaurants);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used cakePHP 1.2 and know how to use behaviors in it. using
Using cakephp, I have a generic address table, which I want to link to
I have an existing web application that I am converting to use CakePHP. The
I've one model, which has several(6) linked tables. Most of times, I use this
I try to use CakePHP 2 to execute model queries in vain. I've got
I use meioupload to upload images in cakePHP, i use a table called 'attachment'
So I have the following example tables: Table A: id name type hat_size created
I have table 'pages' in my database. I have noticed that cakePHp has a
So I have three tables and subsequently three models as well: Company (table: companies)
I have A MySQL database currently in production use for a CakePHP application A

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.