0

Hey my problem is to define a condition for a HABTM Model

(int) 0 => array(
    'Article' => array(
        'id' => '',
        'title' => '',
        'body' => '',
        'created' => null,
        'modified' => null
    ),
    'Channel' => array(
        'id' => '',
        'channelname' => '',
        'created' => null,
        'modified' => null
    ),
    'Tag' => array(
        (int) 0 => array(
                'id' => '1',
                'value' => 'example',
        ),
        (int) 1 => array(
            [maximum depth reached]
        ),
        (int) 2 => array(
            [maximum depth reached]
        )
    ),
),

I want to define a condition that searches the Tag values for specific values .. like that:

 $this -> Article -> find('all', array('conditions' => array('Tag.0.value' => 'test'))

So does someone knows how to "foreach" that array in this condition? thanks (:

tobysas
  • 308
  • 5
  • 18

1 Answers1

0

You either need to:

  1. Run your find from the other direction (using CakePHPs awesome Containable Behavior to get the Articles):

    $this->Tag->find('all', array( 'conditions' => array( 'Tag.value' => 'test' ), 'contain' => array( 'Article' ) ));

  2. Or, use JOINs. Run your find like you're doing, but do an INNER JOIN on tags where the tag value='test'

You cannot limit the main Model's results based on an associated model's conditions. The reason is, when you rely on recursive, or use Contain, CakePHP actually creates separate queries to pull in your different models' data. Therefore, you can't have a condition in one query affect the other query.


See similar question/answers:

How do I restrict query results based on sub-results in CakePHP?

Conditions in JOINed tables shows error CakePHP

cakephp contain filtering by parent model value

cakephp contain association conditions issue

Adding conditions to Containable in CakePHP

Community
  • 1
  • 1
Dave
  • 28,833
  • 23
  • 113
  • 183