Sylius和Doctrine表达式构建器 – 多字段过滤

huangapple go评论61阅读模式
英文:

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')
            ;
    }

huangapple
  • 本文由 发表于 2023年6月21日 23:16:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76524837.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定