Felipe Xavier Felipe Xavier - 1 month ago 4x
jQuery Question

how to make a dynamic query using cakephp 3?

I have a filter to search the database from it. But if any of the filter fields are empty it is to ignore.

$name = $this->request->data['name'];
$status = $this->request->data['status'];
$code_transaction = $this->request->data['code_transaction'];

$orders = $this->Orders->find('all')->where(['name'=> $name, 'status'=> $status, 'id'=> $code_transaction]);

when you have a blank field behind not query anything database.
I need help to create a query to do the query in the database when you first filled field and ignore those that are blank. Could anyone help me? I accept any idea to create it.


You can do something like this:

$query = ['name'=> $name, 'status'=> $status, 'id'=> $code_transaction];
$newdataquery =[];
foreach($query as $key => $data) {
 if (strlen($data) > 0 )
     $newdataquery[$key] = $data;
$orders = $this->Orders->find('all')->where($newdataquery);