<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
chocofamilyme / phalcon-query-builder-filters example snippets
namespace RestAPI\Models\Filters;
use Chocofamily\QueryBuilderFilters\BaseFilter;
class UserFilter extends BaseFilter
{
public function gender($value)
{
return $this->builder->andWhere('RestAPI\Models\User.gender = :gender:', [
'gender' => $value
]);
}
public function working($value)
{
return $this->builder->andWhere('RestAPI\Models\Profile.is_working = :isWorking:', [
'isWorking' => $value
]);
}
}
namespace RestAPI\Models;
use Chocofamily\QueryBuilderFilters\Contracts\HasFilters;
use RestAPI\Models\Filters\UserFilter;
use Phalcon\Mvc\Model;
class User extends Model implements HasFilters
{
/**
* @return string
*/
public function getFilterClass(): string
{
return UserFilter::class;
}
}
public function getFilteredUsers()
{
$filterHandler = new FilterHandler();
$filters = [
'is_working' => true,
'gender' => 1,
'not_exist_column' => 'test' // так как такого метода у нас нету, этот фильтр проигнорируется
];
$builder = $this->modelsManager->createBuilder()
->columns(
[
'RestAPI\Models\User.id',
'RestAPI\Models\User.name',
'RestAPI\Models\User.gender',
'RestAPI\Models\Profile.is_working',
'RestAPI\Models\Profile.is_active'
]
)
->from('RestAPI\Models\User')
->innerJoin('RestAPI\Models\Profile', 'RestAPI\Models\User.id = RestAPI\Models\Profile.user_id')
->orderBy('is_active DESC');
$filteredBuilder = $filterHandler->handle($builder, $filters);
return $filteredBuilder->getQuery()->execute();
}
use Chocofamily\QueryBuilderFilters\FilterHandler;
$di = \Phalcon\Di::getDefault();
$di->set('query-builder-filter', function () use ($di) {
return new FilterHandler();
});
$this-> builder
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.