2

I have the folowing associations

post->primary->secondary

$results = $this->Post->find('all', array(
    'conditions' => array(
        'Post.post_id =' => 2,
        'Primary.secondary_id !=' => null
    ),
    'contain' => array(
        'Primary' => array(
            'Secondary' => array(
                'conditions' => array('Secondary.short_code =' => 'code')
            )
        )
    )
));

Returns this.

Array
(
    [0] => Array
        (
            [Post] => Array
                (
                    [id] => 2
                    [created] => 2012-10-29 09:48:29
                    [modified] => 2012-10-29 09:48:29
                )

            [Primary] => Array
                (
                    [id] => 3
                    [secondary_id] => 6
                    [Secondary] => Array
                        (
                            [id] => 6
                            [short_code] => code
                            [created] => 2012-10-31 11:19:56
                            [modified] => 2012-10-31 11:20:03
                        )

                )

        )

However when I change

'conditions' => array('Secondary.short_code =' => 'code')

to

'conditions' => array('Secondary.short_code !=' => 'code')

it still returns the primary record, when I dont want it to.

Array
(
    [0] => Array
        (
            [Post] => Array
                (
                    [id] => 2
                    [created] => 2012-10-29 09:48:29
                    [modified] => 2012-10-29 09:48:29
                )

            [Primary] => Array
                (
                    [id] => 3
                    [secondary_id] => 6
                    [Secondary] => Array
                        (
                        )

                )

        )
madphp
  • 1,716
  • 5
  • 31
  • 72
  • What exactly are you trying to accomplish? – bbb Oct 31 '12 at 18:52
  • I want to limit the results returns. Doesnt cakephp containable behaviour do that? http://book.cakephp.org/1.3/view/1323/Containable – madphp Oct 31 '12 at 19:00
  • 1
    Check out Dave's answer then. Containable serves as a convenient tool for limiting/formatting the results array, once the query has been executed. You're looking for a limit/join applied on query execution. – bbb Oct 31 '12 at 19:04
  • @bbb: from bakery - **"This model behavior allows you to filter and limit model find operations. Using Containable will help you cut down on needless wear and tear on your database, increasing the speed and overall performance of your application."** So, it doesn't filter after running queries, instead, it just runs required queries on DB and filters/formats results thus reducing load on DB. – Fr0zenFyr Nov 03 '15 at 11:57

1 Answers1

5

It's hard to know exactly what you're hoping to achieve, but I THINK it sounds like you're trying to limit the 'Primary' results based on conditions against the 'Secondary' model.

If that's the case, you're going to need to use joins() instead of contain().

The reason: When you use CakePHP's Containable Behavior, it actually does separate queries, then combines the results before returning the data to you. Doing it this way is great in many ways, but it does not allow you to limit parent results based on conditions against it's children.

To do that, just use joins(). (CakePHP syntax which creates MySQL JOIN)

Dave
  • 28,833
  • 23
  • 113
  • 183