zoid/doctrine-filter-objects

此包最新版本(dev-master)没有可用的许可证信息。

维护者

详细信息

gitlab.com/zoid/dfo

源代码

问题

安装: 31

依赖: 0

建议者: 0

安全: 0

星标: 0

分支: 0

dev-master 2019-07-28 20:21 UTC

This package is not auto-updated.

Last update: 2024-09-30 23:34:59 UTC


README

包装在Doctrine\ORM的QueryBuilder上。提供易于使用、可定制的类来指定WHEREORDER参数。

过滤

创建通用过滤器

$data = [
    'name' => 'David',
    'location' => 'USA',
    'age' => 26,
    'position' => 'IT'
];

$filter = new QueryFilter($data);

// You can specify the relations between parameters
// Untouched parameters will be appended to the end of query with AND operator

$filter ->and('name')
        ->and('location')
            ->or('age', Where::NOT_EQUAL)
            ->end()
        ->and('position', Where::LIKE);
        
// Return DQL Where part with prefixed columns
 
echo $filter->getStatement('c'); 

// "c.name = David AND ((c.location = USA OR c.age <> 26) AND c.position LIKE Manager)"

在查询构建器中使用过滤器

由于$filter->getStatement()返回string,因此它很容易在Doctrine QueryBuilder中使用。

$qb = $this->em->createQueryBuilder();
$qb ->select('p')
    ->from(Product::class, 'p')
    ->where($filter->getStatement('p');

您可以通过使用预定义类FilterApplier来跳过传递$prefix参数

$qb = $this->em->createQueryBuilder();
$qb ->select('p')
    ->from(Product::class, 'p');
    
$applier = new FilterApplier($qb);
$applier->filter($filter);

创建自定义过滤器

您可以通过实现IQueryFilter接口来创建自定义过滤器

class ActiveProductsFilter implements IQueryBuilderFilter
{
	public function getStatement(string $prefix): string
	{
		$expr = new Expr();

		return $expr->andX(
			$expr->eq("{$prefix}.removed", 'FALSE'),
			$expr->eq("{$prefix}.visible", 'TRUE')
		);
	}
}

并轻松地在FilterApplier中使用它。

$qb = $this->em->createQueryBuilder();
$qb ->select('p')
    ->from(Product::class, 'p');

$applier = new FilterApplier($qb);
$applier->filter($filter)
    ->filter(new ActiveProductsFilter());

排序

使用QueryOrder

QueryOrder是一个简单的对象,用于存储QueryBuilder的排序选项。

$order = new QueryOrder(['position', 'ASC'])

$qb = $this->em->createQueryBuilder();
$qb ->select('p')
    ->from(Product::class, 'p');
    
// By calling
$qb->orderBy($order->getColumnName('p'), $order->getOrder());

// By FilterApplier
$applier->order($order);