英文:
Sylius and Doctrine expression builder - multiple fields filter
问题
<?php
declare(strict_types=1);
namespace App\Component\App\Grid\Filter;
use Sylius\Component\Grid\Data\DataSourceInterface;
use Sylius\Component\Grid\Filtering\FilterInterface;
final class MultiselectFilterAuthor implements FilterInterface
{
public function apply(DataSourceInterface $dataSource, string $name, $data, array $options): void
{
if (empty($data)) {
return;
}
$field = $options['field'] ?? $name;
$expressionBuilder = $dataSource->getExpressionBuilder();
$expressions[] = $expressionBuilder->isNull('author');
$expressions[] = $expressionBuilder->in('author.id', [2]);
$dataSource->restrict($expressionBuilder->orX(...$expressions));
}
}
英文:
I am using Sylius 1.11 and I am trying to build a custom filter based on a custom field of the native Order entity.
The field is called author
and it reference the adminUser_id. Sometimes, there is no adminUser_id so the field is null
in the database.
I want to create a multi-select filter to find orders when adminUser_id equals 2 and is null.
So, here is the filter :
<?php
declare(strict_types=1);
namespace App\Component\App\Grid\Filter;
use Sylius\Component\Grid\Data\DataSourceInterface;
use Sylius\Component\Grid\Filtering\FilterInterface;
final class MultiselectFilterAuthor implements FilterInterface
{
public function apply(DataSourceInterface $dataSource, string $name, $data, array $options): void
{
if (empty($data)) {
return;
}
$field = $options['field'] ?? $name;
$expressionBuilder = $dataSource->getExpressionBuilder();
$expressions[] = $expressionBuilder->isNull('author');
$expressions[] = $expressionBuilder->in('author.id', [2]);
$dataSource->restrict($expressionBuilder->orX(...$expressions));
}
}
If I try each expressions independently, it works.
$expressions[] = $expressionBuilder->isNull('author');
-> show only the orders with an author === null
but when I combine them, it doesn't works
答案1
得分: 0
我终于找到了为什么它不起作用的原因...
我需要在订单实体的Sylius网格查询构建器中添加一个author
连接:
public function createListQueryBuilder(): QueryBuilder
{
$qb = $this->createQueryBuilder('o');
return $qb
->addSelect('customer')
->leftJoin('o.customer', 'customer')
->leftJoin('o.author', 'author')
->leftJoin('customer.contacts', 'contacts')
;
}
英文:
I finally found out why it wasn't working...
I needed to add an author
join in the Sylius grid query builder for the Order entity :
public function createListQueryBuilder(): QueryBuilder
{
$qb = $this->createQueryBuilder('o');
return $qb
->addSelect('customer')
->leftJoin('o.customer', 'customer')
->leftJoin('o.author', 'author')
->leftJoin('customer.contacts', 'contacts')
;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论