在cakephp 3

时间:2017-08-26 07:32:53

标签: cakephp query-builder cakephp-3.4

我正在编写一个带有搜索功能的应用程序。有许多过滤器要应用,因此我想在find()应用程序

中的CakePHP 3.4函数之外构建过滤器查询

这就是我想要实现的目标

$start_year = $this->request->getQuery('start_year');
$end_year = $this->request->getQuery('end_year');
$keyword = $this->request->getQuery('keyword');
$make = $this->request->getQuery('make');

$query_builder = [];

if (!empty($keyword)) {
    $query_builder['keyword'] = $keyword;
}
if (!empty($make)) {
    $query_builder['make'] = $make;
}

if (!empty($start_year) && empty($end_year))
{
    $query_builder['year >'] = $start_year;
}

if (empty($start_year) && !empty($end_year)) {
    $query_builder['year <'] = $end_year;
}

if (!empty($start_year) && !empty($end_year)) {
    // how to written in BETWEEN query here on year column
}

$results = $this->Model->find()
    ->where($query_builder);

如何在数组中为IN BETWEEN查询构建查询?

1 个答案:

答案 0 :(得分:1)

if (!empty($start_year) && !empty($end_year)) {
    $query_builder['year >='] = $start_year;
    $query_builder['year <='] = $end_year;
}