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

  • Home
  • SEARCH
  • 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 3460556
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:14:39+00:00 2026-05-18T10:14:39+00:00

I have a table called Landmarks and a table called Categories with a HABTM

  • 0

I have a table called Landmarks and a table called Categories with a HABTM relationship to each other. I am trying to retrieve all the categories for a specific Landmark with the following code:

$this->set('selected_categories', $this->Category->find('list',
    array('contain'=>array(
        'Landmarks'=>array(
           'conditions'=>array('Landmark.num'=>7)
)))));

I’ve tested the database query that results from this (as printed out in debug level 2), and it seems to get the right results, i.e. a subset of the Categories. However, when I var_dump $selected_categories in the view, I’m getting a list of ALL categories instead.

Am I missing something obvious here?

ETA: I told a lie when I said the above query was working. Something else on the page is generating the SQL query I want! Which is:

SELECT `Categories`.`num`, `CategoriesLandmark`.`category_num`,
  `CategoriesLandmark`.`landmark_num` 
FROM `categories` AS `Categories` JOIN `categories_landmarks` 
AS `CategoriesLandmark` ON (`CategoriesLandmark`.`landmark_num` = 7
AND `CategoriesLandmark`.`category_num` = `Categories`.`num`) WHERE 1 = 1
  • 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-05-18T10:14:39+00:00Added an answer on May 18, 2026 at 10:14 am

    Specifying the find type as ‘list’ is incompatible with the Containable behavior.

    Use joins instead to accomplish this:

    $selectedCategories = $this->Category->find('list', array(
        'joins' => array(
            array(
                'table' => 'categories_landmarks',
                'alias' => 'CategoriesLandmark',
                'type' => 'inner',
                'conditions' => array("CategoriesLandmark.category_id = Category.id")
            ),
            array(
                'table' => 'landmarks',
                'alias' => 'Landmark',
                'type' => 'inner',
                'conditions' => array(
                    "CategoriesLandmark.landmark_id = Landmark.id",
                    'Landmark.num' => 7
                )
            ),
        )
    ));
    

    I would ideally place this directly into the Category model like so:

    function fetchListByLandmarkNum($landmarkNum) {
        return $this->find('list', array(
            'joins' => ...
        ));
    }
    

    And then call it from the controller:

    $selectedCategories = $this->Category->fetchListByLandmarkNum(7);
    $this->set(compact('selectedCategories'));
    

    Always make your models fat and your controllers skinny. You can now reuse this function elsewhere. 🙂


    The reason why ALL the categories are being returned is that the condition is applied on the related Landmark models. The result of this Containable query would be to fetch all the Categories, and return only those Landmark models which satisfy num = 7. It should NOT be interpreted as returning only those Categories for which the Landmark models satisfy the condition.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table called BlogPost which has a 1-to-many relationship with the Comment
I have a database table called Posts which stores all the information regarding an
I have a table called OffDays, where weekends and holiday dates are kept. I
I have a Table called Product and I have the Table StorageHistory . Now,
I have a table called ApprovalTasks... Approvals has a status column I also have
i have a table called category in which i have main category ids and
Suppose I have a table called Companies that has a DepartmentID column. There's also
Say I have a table called myTable. What is the SQL command to return
Say I have a table called xml that stores XML files in a single
Right now I have a table called Campaigns that has many Hits, if I

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.