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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:08:25+00:00 2026-05-27T03:08:25+00:00

I’ve been quite some time trying to use the Containable Behavior in CakePHP but

  • 0

I’ve been quite some time trying to use the Containable Behavior in CakePHP but I can’t get to make it work as I expected.

My application is different, but to simplify I’ll put this example. Let’s say I have a forum with threads and activities, and the activities can be rated. The general relations would be:

Forum: hasMany [Thread]
Thread: belongsTo [Forum], hasMany [Activity]
Activity: belongsTo [Thread], hasMany [Rating]
Rating: belongsTo [Activity]

What I want to achieve is, using the find method, get all the ratings performed on a certain forum. What I suppose should be done is the following:

 $this->Rating->find('count', array(
    'contain' => array(
        'Activity' => array(
            'Thread'
        )
    ),
    'conditions' => array(
        'Thread.forum_id' => 1
    )
));

But the result query is:

SELECT COUNT(*) AS `count` FROM `ratings` AS `Rating` LEFT JOIN `activities` AS `Activity` ON (`Rating`.`activity_id` = `Activity`.`id`) WHERE `Thread`.`forum_id` = 1;

I’ve accomplished this using the ‘joins’ option, but it’s more complex and I have to use this kinda action in many situations.

All the files related with the example can be found here: http://dl.dropbox.com/u/3285746/StackOverflow-ContainableBehavior.rar

Thanks

Update 23/11/2011

After investigating the framework and thanks to the answers of Moz Morris and api55 I found the source of the problem.

The basic problem was that, as I understood CakePHP, I thought it was querying using joins each time. The thing it that it doesn’t do that, the real operation it would perform to obtain the result I was looking for would be something like this:

SELECT * FROM Rating JOIN Activity...
SELECT * FROM Activity JOIN Thread...
SELECT * FROM Activity JOIN Thread...
...

Meaning that it would do a query to get all the activities and then, for each activity, perform a query to get the Threads… My approach was failing not because of the Containable Behaviour being used wrong, but because the ‘conditions’ option was applied to all queries and, on the first one, it crashed because of the absence of the Thread table. After finding this out, there are two possible solutions:

  • As api55 said, using the conditions inside the ‘contain’ array it would apply them only to the queries using the Thread table. But doing this the problem persists, because we have way too many queries.

  • As Moz Morris said, binding the Thread model to Rating would also work, and it would perform a single query, which is what we want. The problem is that I see that as a patch that skips the relations betweem models and doesn’t follow CakePHP philosophy.

I marked api55 solution as the correct because It solves the concrete problem I had, but both give a solution to the problem.

  • 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-27T03:08:26+00:00Added an answer on May 27, 2026 at 3:08 am

    First of all, have you put the actAs containable variable in the appModel?? without it this beahaviour won’t work at all (i see it is not working correctly since it didn’t join with Thread table)

    I would do it from the top, i mean from forum, so you choose your forum (im not sure you want forum or thread) and get all its rating, if theres no rating you will end up with the rating key empty.

    something like this

    appModel

    public $actsAs = array('Containable');
    

    rating controller

     $this->Rating->Activity->Thread->Forum->find('count', array(
        'contain' => array(
            'Thread' => array(
                 'Activity' => array(
                     'Rating' => array (
                           'fields' => array ( 'Rating.*' )
                      )
                  )
            )
        ),
        'conditions' => array(
            'Forum.id' => 1
        )
    ));
    

    Then if you need only a value in rating table just use Set:extract to get an array of this value.

    As you did it IT SHOULD work anyways but i sugest not to use forum_id there, but in conditions inside contain like this

    'contain' => array(
        'Activity' => array(
            'Thread' => array(
                  'conditions' => array('Thread.forum_id' => 1)
             )
        )
    ),
    

    Also, never forget the actsAs variable in the model using the containable behaviuor (or in app model)

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I want to construct a data frame in an Rcpp function, but when 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.