对ManyToMany关系进行多值过滤

时间:2019-05-15 10:11:45

标签: symfony4

我有一个通过ManyToMany关系链接到多个主题实体的上下文实体。 我正在使用LexikFormFilter尝试使用多个主题ID值来过滤那些上下文

这是我的ContextFilterType

class ContextFilterType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('id', Filters\NumberFilterType::class, [
            'condition_operator' => FilterOperands::OPERATOR_EQUAL
        ]);

        $builder->add('name', Filters\TextFilterType::class, [
            'condition_pattern' => FilterOperands::STRING_CONTAINS
        ]);

        $builder->add('state', Filters\BooleanFilterType::class);

        $builder->add('themes', Filters\CollectionAdapterFilterType::class, [
            'entry_type' => ThemeFilterType::class,
            'add_shared' => function (FilterBuilderExecuterInterface $qbe)  {
                $closure = function (QueryBuilder $filterBuilder, $alias, $joinAlias, Expr $expr) {
                    $filterBuilder->leftJoin($alias . '.themes', $joinAlias);
                };

                // then use the query builder executor to define the join and its alias.
                $qbe->addOnce($qbe->getAlias().'.themes', 'th', $closure);
            },
        ]);
    }

    public function getBlockPrefix()
    {
        return 'filter';
    }

    public function getParent()
    {
        return Filters\SharedableFilterType::class; // this allow us to use the "add_shared" option
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'csrf_protection'   => false,
            'validation_groups' => array('filtering'),
//            'method' => 'get',
        ));
    }
}

我的ThemeFilterType

class ThemeFilterType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('id', Filters\NumberFilterType::class, [
            'condition_operator' => FilterOperands::OPERATOR_EQUAL
        ]);


        $builder->add('name', Filters\TextFilterType::class, [
            'condition_pattern' => FilterOperands::STRING_CONTAINS
        ]);

        $builder->add('state', Filters\BooleanFilterType::class);
    }

    public function getBlockPrefix()
    {
        return 'filter';
    }

    public function getParent()
    {
        return Filters\SharedableFilterType::class; // this allow us to use the "add_shared" option
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'csrf_protection'   => false,
            'validation_groups' => array('filtering')
        ));
    }
}

这是请求: /contexts?filter[themes][0][name]=Begue&filter[themes][1][name]=Guichard

不幸的是,它仅返回与主题“ Begue”(过滤器的第一个值)相关的上下文 这是生成的SQL

SELECT c0_.id AS id_0, c0_.name AS name_1, c0_.presentation AS presentation_2, c0_.vision_teacher AS vision_teacher_3, c0_.vision_learner AS vision_learner_4, c0_.state AS state_5, c0_.deleted AS deleted_6, c0_.created_at AS created_at_7, c0_.updated_at AS updated_at_8, c0_.client_id AS client_id_9
FROM context c0_
LEFT JOIN context_theme c2_ ON c0_.id = c2_.context_id
LEFT JOIN theme t1_ ON t1_.id = c2_.theme_id AND (t1_.deleted = '0')
WHERE (t1_.name LIKE '%Begue%' AND c0_.client_id = ?) AND (c0_.deleted = '0')

0 个答案:

没有答案