views:

19

answers:

1

I have two models that I would like to save in the same table. For instance I have a status model and a payschedule model both should be saved in the statuses table. But at retrieving the status model should return only the records with payment = 'no' and the payschedule only records with payment = 'yes'. I will have a before save in each model to make sure that the correct payment value is saved to the table. My question is how can I restrict the retrieval from the table on the model to the constraints explained above without having to do it at each find() operation?

Any help would be greatly appreciated.

ps I you have not figured it out, I am a CakePHP noob.

A: 

It should be possible to implement this in the find() method of your model:

public function find($type, $options = array()) {

    // Make sure there is a 'conditions' array.
    if(!isset($options['conditions']))
        $options['conditions'] = array();

    // Overwrite conditions in $options with your automatic conditions.
    $options['conditions'] = array_merge(
        $options['conditions'],
        array('payment' => 'yes')
    );

    // Just pass them to the parent implementation.
    return parent::find($type, $options);
}

edit:

To follow the CakePHP recommendation, it should probably be implemented in the function beforeFind() instead: http://book.cakephp.org/view/1049/beforeFind

geon
Thanks! Will try it and feedback.
RaScoop
Thanks! Works like a charm! ... In retrospect I can only think of one thing .... DUH!
RaScoop