I'm using phalcon-datatables this is a datatables adapter for Phalcon PHP.
I have a where condition in my QueryBuilder, e.g :
use \DataTables\DataTable;
class TestController extends \Phalcon\Mvc\Controller {
public function indexAction() {
if ($this->request->isAjax()) {
$builder = $this->modelsManager->createBuilder()
->columns('id, name, email, age')
->from('Example\Models\User');
->where("age = :age:", array("age" => 30))
$dataTables = new DataTable();
$dataTables->fromBuilder($builder)->sendResponse();
}
}
}
But I have a problem when I'm using the global search. The where condition is ignored then it will show me all record in my database with the search condition.
When I'm using the column search it works well.
I think the problem is in this class -> Github line 21-23
For the global search we need to get all column and this library use orWhere for the global search.
$this->bind('global_search', function($column, $search) {
$this->builder->orWhere("{$column} LIKE :key_{$column}:", ["key_{$column}" => "%{$search}%"]);
});
And the column search use andWhere.
$this->bind('column_search', function($column, $search) {
$this->builder->andWhere("{$column} LIKE :key_{$column}:", ["key_{$column}" => "%{$search}%"]);
});
So I think this is why the column search works with a where condition in the QueryBuilder. And this is why the global search doesn't work.
Do you have any idea to fix this problem inside the library ?
EDIT :
I think I have a solution. I have to put brackets on the orWhere.
But how can I do to make this query with phalcon ?
SELECT *
FROM `users`
WHERE age = 30
AND (name like '%d%'
OR email like '%d%'
OR login like '%d%')