nebkam/odm-search-param
最新稳定版本:6.0.9
Composer 安装命令:
composer require nebkam/odm-search-param
包简介
README 文档
README
ODM Search Param
Rationale
- you use Doctrine MongoDB ODM
- you need some search functionality, that can be expressed through the
QueryBuilderor theMatchPhaseof the Aggregation Builder - you have a class that holds the search parameters (i.e. a domain class in Symfony)
- you don't want to manually map the search parameters to builder statements
Usage
- Search filter class can be any class with public properties
- Properties should be marked with
#[SearchParam]attribute - Use the
SearchParamParser::parsewith aQueryBuilder|MatchStageinstance to build the query on
Examples
Verbatim (String and Bool types)
class SearchFilter { #[SearchParam(type: SearchParamType::String)] public string $name; }
Builds the query to:
$builder->field('name')->equals($propertyValue);
Using different property names
class SearchFilter { #[SearchParam(type: SearchParamType::Exists, field: 'images')] public bool $hasImages; }
Builds the query to:
$builder->field('images')->exists(true);
Type casting (Int type)
class SearchFilter { #[SearchParam(type: SearchParamType::Int)] public $age; }
Builds the query to:
$builder->field('age')->equals((int) $propertyValue);
Type casting with arrays (IntArray and StringArray types)
class SearchFilter { /** * @var int[] */ #[SearchParam(type: SearchParamType::IntArray)] public array $grades; }
Builds the query to:
$builder->field('grades')->in($propertyValuesAllCastedToInt);
Using backing values from enums (StringEnum and IntEnum types)
class SearchFilter { #[SearchParam(type: SearchParamType::StringEnum)] public SideOfTheWorldEnum $sideOfTheWorld; }
Builds the query to:
$builder->field('sideOfTheWorld')->equals($propertyValue->value);
Using backing values from enum arrays (StringEnumArray and IntEnumArray types)
class SearchFilter { /** * @var SideOfTheWorldEnum[] */ #[SearchParam(type: SearchParamType::StringEnumArray)] public array $sidesOfTheWorld; }
Builds the query to:
$builder->field('sideOfTheWorld')->in($backingValuesOfPropertyValue);
Querying by range (RangeInt, RangeInt and RangeFloat types)
class SearchFilter { #[SearchParam(type: SearchParamType::RangeInt, direction: SearchParamDirection::From)] public int $price; }
Builds the query to:
$builder->field('price')->gte((int) $propertyValue);
Custom query building by specifying the callable
class SearchFilter { #[SearchParam(type: SearchParamType::Callable, callable: [SomeClass::class, 'setStatus'])] public string $status; } class SomeClass { public static function setStatus(Builder|MatchStage $builder, $value, $filter) { // Call $builder methods to build the query } }
Testing
- run
docker.sh
统计信息
- 总下载量: 1.44k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-07-04