paliari / doctrine-ransack
Doctrine ORM 的高级查询搜索(基于 Ruby on Rails Ransack)
2.1.0
2022-05-14 18:44 UTC
Requires
- php: >=8.0
- doctrine/orm: ^2.8
Requires (Dev)
- fzaninotto/faker: ^1.5
- paliari/php-utils: ^2.0
- phpunit/phpunit: ^9.5
README
安装
$ composer require paliari/doctrine-ransack
配置
您的设置,示例
<?php use Paliari\Doctrine\Ransack; use Paliari\Doctrine\RansackConfig; $ransack = new Ransack(new RansackConfig($entityManager));
使用
<?php use Paliari\Doctrine\Ransack; use Paliari\Doctrine\RansackConfig; use Paliari\Doctrine\VO\RansackOrderByVO; use Paliari\Doctrine\VO\RansackParamsVO; $entityName = User::class; $alias = 't'; $paramsVO = new RansackParamsVO(); $paramsVO->where = [ 'person.address.street_cont' => 'Av% Brasil', 'or' => [ 'name_eq' => 'Jhon', 'email_start' => 'jhon', 'person.address.city_eq' => 'Maringá', ], 'id_order_by' => 'asc', ]; $paramsVO->orderBy = [ new RansackOrderByVO(['field' => 'person.name', 'order' => 'ASC']), new RansackOrderByVO(['field' => 'person.id', 'order' => 'DESC']), ]; $paramsVO->groupBy = [ 'person.name', 'person.address_id', ]; $qb = $entityManager->createQueryBuilder()->from($entityName, $alias); $ransackBuilder = $this->ransack ->query($qb, $entityName, $alias) ->includes() ->where($paramsVO); $users = $ransackBuilder->getQuery()->getResult(); // Using includes $includes = [ 'only' => ['id', 'email'], 'include' => [ 'person' ['only' => ['id', 'name']], ], ]; $rows = $ransackBuilder->includes($includes)->getArrayResult();
自定义关联
您自定义关联的类
<?php use Doctrine\ORM\Query\Expr\Join; use Paliari\Doctrine\CustomAssociationInterface; use Paliari\Doctrine\VO\RelationVO; use Paliari\Doctrine\VO\JoinVO; use Person; use User; class CustomAssociation implements CustomAssociationInterface { public function __invoke(string $entityName, string $alias, string $field): ?RelationVO { if (User::class === $entityName && 'custom' == $field) { $relationVO = new RelationVO(); $relationVO->entityName = $entityName; $relationVO->fieldName = $field; $relationVO->targetEntity = Person::class; $joinVO = new JoinVO(); $joinVO->join = Person::class; $joinVO->alias = "{$alias}_$field"; $joinVO->conditionType = Join::WITH; $joinVO->condition = "$alias.email = $joinVO->alias.email"; $relationVO->join = $joinVO; return $relationVO; } return null; } }
使用 CustomAssociation 进行设置
<?php use Paliari\Doctrine\Ransack; use Paliari\Doctrine\RansackConfig; $customAssociation = new CustomAssociation(); $config = new RansackConfig($entityManager, $customAssociation); $ransack = new Ransack($config); $entityName = User::class; $alias = 't'; $paramsVO = new RansackParamsVO(); $paramsVO->where = [ 'custom.email_eq' => 'your-email@gmail.com', ]; $includes = [ 'only' => ['id', 'email'], 'include' => [ 'custom' ['only' => ['id', 'name']], ], ]; $qb = $entityManager->createQueryBuilder()->from($entityName, $alias); $ransackBuilder = $this->ransack ->query($qb, $entityName, $alias) ->includes() ->where($paramsVO); $users = $ransackBuilder->getQuery()->getResult();
过滤器
过滤器必须以哈希表的形式传递,其中键名包含以下谓词的字段名,例如: person.name_eq
,person.id_gt
。
也可以在 or
或 and
子句内组合谓词,例如
$where = [ 'name_cont' => 'Jhon', 'or' => [ 'person.name_start' => 'Jhon', 'person.email_end' => '@gmail.com', 'and' => [ 'person.address.city_eq' => 'Maringá', 'person.address.state_eq' => 'PR', ], ], ];
所有可能的谓词列表
-
*_eq(等于)
-
示例
{"col_eq": "Fulano da Silva"}
-
SQL 结果
WHERE col = 'Fulano da Silva'
-
-
*_not_eq(不等于)
-
示例
{"col_not_eq": "Fulano da Silva"}
-
SQL 结果
WHERE col <> 'Fulano da Silva'
-
-
*_in(匹配数组中的任何值)
-
示例
{"col_in": [13, 21, 124, 525]}
-
SQL 结果
WHERE col IN (13, 21, 124, 525)
-
-
*_not_in(不匹配数组中的任何值)
-
示例
{"col_not_in": [13, 21, 124, 525]}
-
SQL 结果
WHERE col NOT IN (13, 21, 124, 525)
-
-
*_null(是空值)
-
示例
{"col_null": null}
-
SQL 结果
WHERE col IS NULL
-
-
*_not_null(不是空值)
-
示例
{"col_not_null": null}
-
SQL 结果
WHERE col IS NOT NULL
-
-
*_present(非空和非空字符串)
仅与字符串列兼容。
-
示例
{"col_present": 1}
-
SQL 结果
WHERE col IS NOT NULL AND col != ''
-
-
*_blank(是空值或空字符串)
仅与字符串列兼容。
-
示例
{"col_blank": 1}
-
SQL 结果
WHERE col IS NULL OR col = ''
-
-
*_lt(小于)
-
示例
{"col_lt": 25}
-
SQL 结果
WHERE col < 25
-
-
*_lteq(小于或等于)
-
示例
{"col_lteq": 25}
-
SQL 结果
WHERE col <= 25
-
-
*_gt(大于)
-
示例
{"col_gt": 25}
-
SQL 结果
WHERE col > 25
-
-
*_gteq(大于或等于)
-
示例
{"col_gteq": 25}
-
SQL 结果
WHERE col >= 25
-
-
*_matches(匹配
LIKE
)-
示例
{"col_matches": "Fulano"}
-
SQL 结果
WHERE col LIKE 'Fulano'
-
-
*_not_matches(不匹配
LIKE
)-
示例
{"col_not_matches": "Fulano"}
-
SQL 结果
WHERE col NOT LIKE 'Fulano'
-
-
*_cont(包含值)
-
示例
{"col_cont": "Fulano"}
-
SQL 结果
WHERE col LIKE '%Fulano%'
-
-
*_not_cont(不包含)
-
示例
{"col_not_cont": "Fulano Silva"}
-
SQL 结果
WHERE col NOT LIKE '%Fulano%Silva%'
-
-
*_start(以...开始)
-
示例
{"col_start": "Fulano"}
-
SQL 结果
WHERE col LIKE 'Fulano%'
-
-
*_not_start(不以...开始)
-
示例
{"col_not_start": "Fulano"}
-
SQL 结果
WHERE col NOT LIKE 'Fulano%'
-
-
*_end(以...结束)
-
示例
{"col_end": "Fulano"}
-
SQL 结果
WHERE col LIKE '%Fulano'
-
-
*_not_end(不以...结束)
-
示例
{"col_not_end": "Fulano"}
-
SQL 结果
WHERE col NOT LIKE '%Fulano'
-
-
*_between(介于两个值之间)
-
示例
{"col_between": [10, 20]}
-
SQL 结果
WHERE col BETWEEN 10 AND 20
-