[英]The correct way to set a default search value in CakePHP
I have a CakePHP Application with many many screens using data from a specific model. 我有一个CakePHP应用程序,其中有许多屏幕都使用特定模型的数据。 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.
大多数时候,我需要检查一个特定的字段(例如食谱中的“ user_active => 1”示例),但是我仍然希望能够将该字段指定为条件并覆盖默认值。 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? 另外,$ queryData字段是混合的-数组以外还有什么可能性? just NULL?
只是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. 我建议您在模型中创建一个自定义的
findCourses($options)
函数。 That way, you can just set all the default conditions, and override any that were passed in $options
. 这样,您可以设置所有默认条件,并覆盖
$options
中传递的任何条件。
Here's an example: 这是一个例子:
//Article model
public function articles($opts = null) {
//initialize
$params = array();
$findType = 'all';
$params['conditions'] = array();
//status
if(!empty($opts['status'])) {
array_push($params['conditions'], array('Article.status'=>$opts['status']));
} else {
array_push($params['conditions'], array('Article.status'=>1));
}
//id
if(!empty($opts['id'])) {
$params['limit'] = 1;
$findType = 'first';
array_push($params['conditions'], array('Article.id'=>$opts['id']));
}
//slug
if(!empty($opts['slug'])) {
$params['limit'] = 1;
$findType = 'first';
array_push($params['conditions'], array('Article.slug'=>$opts['slug']));
}
//limit (and find type)
if(!empty($opts['limit'])) {
$params['limit'] = $opts['limit'];
if($opts['limit'] == 1) $findType = 'first';
}
//order by
$params['order'] = array('Article.created DESC');
if(!empty($opts['order'])) {
$params['order'] = $opts['order'];
}
//type
if(!empty($opts['type'])) {
array_push($params['conditions'], array('type'=>$opts['type']));
}
//find type
if(!empty($opts['find_type'])) $findType = $opts['find_type'];
//slug
if(!empty($opts['slug'])) {
array_push($params['conditions'], array('slug'=>$opts['slug']));
}
//search
if(!empty($opts['search'])) {
array_push($params['conditions'], array('Article.title LIKE' => '%'.$opts['search'].'%'));
}
//contain
$params['contain'] = array('MetaData', 'Tag', 'Upload');
//paginate options or results
if(!empty($opts['paginate'])) {
return $params;
} else {
return $this->find($findType, $params);
}
}
In my controller, I can just do this: 在我的控制器中,我可以这样做:
$opts = array('limit'=>6, 'type'=>'gaming');
$articles = $this->Article->articles($opts);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.