I have a CakePHP Application with many many screens using data from a specific model. Most of the time, I need a specific field to be checked against (like the “user_active=>1” example in the cookbook), however I’d still like to be able to specify that field as a condition and override the default. My code works, but is this the best way to do this?
Also, the $queryData field is mixed — what are the possibilities other than array? just NULL?
<?php public function beforeFind($queryData) {
if(is_array($queryData)) {
//query data was an array
if(!is_array($queryData['conditions'])) {
$queryData['conditions'] = array('Course.semester_id'=>SEMESTER_ID);
} else if (!isset($queryData['conditions']['Course.semester_id'])) {
$queryData['conditions']['Course.semester_id'] = SEMESTER_ID;
}
}
} ?>
I would recommend making a custom
findCourses($options)function in your model. That way, you can just set all the default conditions, and override any that were passed in$options.Here’s an example:
In my controller, I can just do this: