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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:52:05+00:00 2026-05-29T23:52:05+00:00

I have read numerous posts, spent hours going over the documents at cakephp.org and

  • 0

I have read numerous posts, spent hours going over the documents at cakephp.org and even tried various practical examples and I cannot seem to get this right. What I have are 3 tables as follows:

  • projects
  • project_tags
  • tags

When someone adds a new project, they choose their tags, which are selected from a predefined list in the tags table. This tag they entered is then stored in project_tags using the project_id and tag_id fields.

Model relations are as follows:

  • Project hasMany projectTag
  • projectTag belongsTo Project
  • ProjectTag belongsTo Tag

Whether or not I have the correct relations is still trivial to me, but I have tried numerous possibilities here.

What I am trying to do is perform a find on all projects of a specific tag. I have done this by doing the following:

$this->Project->contain(array('ProjectTag' => 'Tag', 'User' => array('id')));
$projects = $this->Project->find('all', array('conditions' => array('ProjectTag.tag_id' => '6')));

The result is an SQL error that column ProjectTag.tag_id does not exist. This tells me that there is an error in the association, but where?

Is this because I cannot use a 2nd level deep associated hasMany model in my fond condition when using containable? If so, how can I correct this?

I know it’s long, sorry. Can anyone shed some light on this bizarre situation?

Another approach I tried was using the unbindModel() on Project, removing all the bindings to ProjectTag, and then adding a hasOne using bindModel() and a condition of ProjectTag.project_id = Project.id but this resulted on 4 records of the same project being returned, one for each ProjectTag, which does not seem right.

I am using the containable behavior and have set recursive = -1 in the AppModel.

Any help is greatly appreciated.
Thanks

  • 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-29T23:52:07+00:00Added an answer on May 29, 2026 at 11:52 pm

    To all who may stumble across this post, trying to include models that are 2+ levels deep in association within your find or paginate query is not a trivial task to say the least.

    The number one point that kept being raised was that CakePHP performs seperate queries for each hasMany and hasAndBelongsToMany associations, making it near impossible to include them in your find conditions.

    I was presented with 2x solutions:

    1) Build a custom query type

    2) Use the unbindModel and bindModel functions and re-create the associations as hasOne

    I opted for point 2 because point 1 seemed questionable when I read the docs (and was only recommended as a last resort) and it allowed me to change the association on the fly, and reset it afterwards, continuing merrily with the associations as I intended.

    For those who are interested, I had a model Project that hasMany ProjectTag that belongsTo Tag. It was not possible to use Tag or ProjectTag in my find types, so I did the following:

    I used the unbindModel() method on Project, ProjectTag and Tag, removing all their associations, essentially unlinking all relationships.

    I then applied the bindModel() to Project, ProjectTag and Tag. Most importantly was the order (hierarchy) I applied these nindings, because I made use of the ‘condition’ property which allowed me to link model ‘A’ to model ‘B’ using SQL WHERE conditions. For example:

    $this->Project->ProjectTag->unbindModel(array(
    'belongsTo' => array('Tag')
    ));
    
    $this->Project->unbindModel(array('hasMany' => array('ProjectTag')));
    
    $this->Project->bindModel(array('hasOne' => array('ProjectTag' => array('foreignKey' => false, 'conditions' => array('ProjectTag.project_id = Project.id')))));
    
    $this->Project->bindModel(array('hasOne' => array('Tag' => array('foreignKey' => false, 'conditions' => array('Tag.id = ProjectTag.tag_id')))));
    

    We can now make use of Tag.id as if it has a hasOne relationship to Project, essentially allowing us to apply find conditions directly on the Tag while returning related Projects.

    It is ESSENTIAL that you unbind all relationships that are used by the models in question, or it will break.

    I hope this helps those who stumble across the same issue.

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

Sidebar

Related Questions

I have read numerous posts and articles about the element in client endpoints on
I have read in some of the ClickOnce posts that ClickOnce does not allow
I have read numerous times that enforcing const-correctness in your C or C++ code
What is the difference between an error and an exception? I have read numerous
I have read through several reviews on Amazon and some books seem outdated. I
I have read a lot that LISP can redefine syntax on the fly, presumably
I have read about partial methods in the latest C# language specification , so
I have read that using database keys in a URL is a bad thing
I have read this post about how to test private methods. I usually do
I have read on Stack Overflow some people that have converting to C#2.0 to

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.