0

I am writing an application with search functionalities. There are many filters to be applied so I want to build the filter query outside the find() function in CakePHP 3.4 Application

This is what I want to achieve

$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);

How to build query in array for IN BETWEEN query ?

1 Answer 1

1
if (!empty($start_year) && !empty($end_year)) {
    $query_builder['year >='] = $start_year;
    $query_builder['year <='] = $end_year;
}
Sign up to request clarification or add additional context in comments.

1 Comment

The second example will not work, binding values doesn't work like that anymore in CakePHP 3.x!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.