Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I exclude records from an associated model in CakePHP?

Tags:

I'm creating a Q&A application in CakePHP, and I want to exclude my associations in some cases. Imagine the following:

I'm listing all questions on the first page using $this->Question->findAll();. Since I have the following association in my model:

public $hasMany = array('Answer' =>
        array('className' => 'Answer',
            'order' => 'Answer.created DESC',
            'foreignKey' => 'post_id',
            'dependent' => true,
            'exclusive' => false,
        )
    );

All answers will get selected at the start page, which is not optimal. How could i do to exclude the answers in this particular method?

Thanks

like image 760
alexn Avatar asked Dec 18 '08 13:12

alexn


1 Answers

I quick look at the CakePHP API reveals that you've got an unbindModel method on the Model. So in you example you can do this:

$this->Question->unBindModel(array('hasMany' => array(’Answer’)))

Alternatively, you can use the Containable behaviour to only select the pieces from MySQL that you require for the current page view.

like image 123
duckyflip Avatar answered Oct 01 '22 20:10

duckyflip