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 7068659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:21:03+00:00 2026-05-28T05:21:03+00:00

I’m just getting started in CakePHP, and am running into trouble limiting retrieved results

  • 0

I’m just getting started in CakePHP, and am running into trouble limiting retrieved results to those where a field in an associated model matches a certain value. I’ve found a lot of related questions here and elsewhere, but they seem to deal with situations that are either more complicated or more simple than my scenario, so here we go:

I have two models that I’m interested in: Image, and Collection, which are associated by a HABTM relationship. The relationship is set up in Image.php and Collection.php correctly through join table collections_images, and I’ve had no issue writing or retrieving data in general. Containable is also set up properly in the Image model.

What I would like to do is run find() on $this->Image, and return only those that belong to a particular collection, whose title is $collection. I have tried this a couple different ways:

$this->Image->contain("Collection"); // to exclude other models "Image" is related to

pr($this->Image->find("all", 
                       array("conditions" => array("Collection.title" => $collection))
                      ));

For this, I get the following error:

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘Collection.title’ in ‘where clause’

It seems you cannot directly establish a search condition through a related model, so I went back and tried to limit my contain as follows:

$this->Image->contain(array(
                          "Collection" => array(
                              "conditions" => array(
                                  "Collection.title" => $collection))));

pr($this->Image->find("all"));

This time I get results, but they include Images that do not belong to the right collection. Here is a simplified snippet that the pr() gives me for $collection = “Urban”:

[3] => Array
    (
        [Image] => Array
            (
                [id] => 39
                [title] => Star Trails over Greenlake
            )

        [Collection] => Array
            (
            )
    )

[4] => Array
    (
        [Image] => Array
            (
                [id] => 40
                [title] => Aurora Bridge and Reflections

            )

        [Collection] => Array
            (
                [0] => Array
                    (
                        [id] => 3
                        [title] => Urban
                        [CollectionsImage] => Array
                            (
                                [id] => 62
                                [collection_id] => 3
                                [image_id] => 40
                            )

                    )

            )

    )

As you can see, it is omitting Collection data for the Images where the Collection title does not match $collection, but it is still including the Image data.

What am I missing here? Is there a way to make either of these two approaches do what I’m aiming for?

For the record, I have also gone about it from the other direction:

$this->Collection->find("all", array("conditions" => array("Collection.title" => $collection)))

which returns just the desired data. However, the array structure of the data it returns creates complications elsewhere in my code, and I’d like to eventually be able to paginate directly off of Image.

Thanks for your time and help, everyone.

EDIT:

After additional searching, I’ve found an acceptable solution here:

http://cakebaker.42dh.com/2007/10/17/pagination-of-data-from-a-habtm-relationship/

This required me to create a model for the mapping table “CollectionsImage.php” and establish hasOne relationships to both Image and Collection. The paginate() is then performed on CollectionsImage, like so:

$data = $this->paginate("CollectionsImage", array("Collection.id" => $collection_id));

This seems a little roundabout and requires an extra step for me in finding the id for a collection with a specified title, so I’m still wondering if anyone knows a more direct solution.

  • 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-28T05:21:04+00:00Added an answer on May 28, 2026 at 5:21 am

    If you want to retrieve the images that belongs to a particular collection, it means that you are actually willing to retrieve a collection with its related images. I would do something like this :

    <?php
        $this->Image->Collection->find('first', array(
          'conditions' => array('Collection.title' => 'Urban'),
          'contain' => array(
            'Image'
          )
        ));
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
For some reason, after submitting a string like this Jack’s Spindle from a text

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.