0

So my current paginate settings look like this:

    public $paginate = array(
        'limit' => 30,
        'contain' => array(
                'Model1' => array(
                        'fields' => array('id')
                ),
                'Model2' => array(
                        'conditions' => array(
                                'Model2.type' => 'Sometype'
                        ),
                        'Model3' => array(
                                'fields' => array('someField')
                        )
                )
        )
);

What I want to achieve is that when the contained Model2 happens to be empty (there is no Model2 with the type 'Sometype'), then this record of Model0 (the parent of Model1 and Model2) should not be included in the returned array of $this->paginate('Model0');.

How could this be achieved?

Oliver
  • 3,981
  • 2
  • 21
  • 35

3 Answers3

0

I'm not sure if that can be achieved with the containable behavior. I know you can do it with manual joins (disclaimer: I may be off in my query, since I haven't tested it):

$results = $this->Model0->find('all', array(
        'conditions' => array(
            'Model2.id <>' => null,
        ),
        'joins' => array(
            array(
                'table' => 'model2',
                'alias' => 'Model2',
                'type' => 'LEFT',
                'conditions' => array(
                    'Model2.type' => 'Sometype',
                    'Model2.model0_id = Model0.id',
                ),
            ),
            array(
                'table' => 'model3',
                'alias' => 'Model3',
                'conditions' => array(
                    'Model3.model2_id = Model2.id'
                ),
            ),
        ),

    ));
Kai
  • 3,803
  • 1
  • 16
  • 33
-1

Set the root condition to require a valid id for Model2.

public $paginate = array(
    'limit' => 30,
    'conditions'=>array(
            'Model2.id >'=>0
    )
    'contain' => array(
            'Model1' => array(
                    'fields' => array('id')
            ),
            'Model2' => array(
                    'conditions' => array(
                            'Model2.type' => 'Sometype'
                    ),
                    'Model3' => array(
                            'fields' => array('someField')
                    )
            )
    )
);

Depending on your model relationships this might not work. It only works for belongsTo and hasOne I believe. In that case, you should start the pagination from Model2 or use bindModel for temporary model relationships.

Nick Zinger
  • 1,174
  • 1
  • 12
  • 28
-1

Set you conditions at the base level of the query rather than inside the contains. Just make sure to prefix them with the model name.

public $paginate = array(
    'conditions' => array(
        'Model2.type' => 'Sometype'
    ),
    'limit' => 30,
    'contain' => array(
        'Model1' => array(
            'fields' => array('id')
         ),
         'Model2' => array(
             'Model3' => array(
                 'fields' => array('someField')
             ),
         ),
    )

);

nbarth
  • 324
  • 1
  • 5