简体   繁体   English

如何在Symfony2中过滤表单集合?

[英]How would one filter a form collection in Symfony2?

I'm rewriting an old application to Symfony2 and I'm a bit stuck. 我正在将旧的应用程序重写为Symfony2,但有点卡住了。 The application has TaskType and TagsType . 该应用程序具有TaskTypeTagsType The TaskType form has a collection of TagType . TaskType形式具有集合TagType Some of the tags might be archived (ie Tag#archived=true ). 某些标签可能已存档(即Tag#archived=true )。 If that's the case, the archived tags should not be shown in the form when you try to edit the task. 在这种情况下,当您尝试编辑任务时,不应在表单中显示已归档的标记。

class TaskType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title')
            ->add(
                'tags',
                'collection',
                array(
                    'type'         => new TagType(),
                    'allow_add'    => true,
                    'allow_delete' => true,
                    'by_reference' => false,
               )
            )
        ;
    }

    // ...
}


class TagType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
        ;
    }

    // ...
}

Is there a way to filter the tags similar to using query_builder when having an entity instead of collection ? entity而不是collection时,有没有办法类似于使用query_builder来过滤标签? Is there any workaround at all? 是否有任何解决方法?

If it is the case for the whole system that archived tags arent shown to the user, you might want to use a global filter since it's similar to "deletable" behaviour. 如果整个系统都将显示给用户的标签存档,则您可能需要使用全局过滤器,因为它类似于“可删除”的行为。

You can look at the code here 您可以在这里查看代码

https://github.com/Atlantic18/DoctrineExtensions/blob/master/lib/Gedmo/SoftDeleteable/Filter/SoftDeleteableFilter.php https://github.com/Atlantic18/DoctrineExtensions/blob/master/lib/Gedmo/SoftDeleteable/Filter/SoftDeleteableFilter.php

Softdeletable lets you ignore "softly deleted" entities, while you want to ignore "archived" tags. Softdeletable允许您忽略“已软删除”的实体,而您想忽略“已存档”的标签。 It's quite advanced example since it uses annotations and such, what you would need to check is only if queried class is "Tag" and simply return " AND Tag.archived = 0" or whatever 这是一个非常高级的示例,因为它使用了注释等,仅当查询的类是“ Tag”并仅返回“ AND Tag.archived = 0”或任何其他值时,您才需要检查

One way would be to mark archived tags as "soft-deleted", other option is to make a collection criteria. 一种方法是将存档标签标记为“软删除”,另一种方法是制定收集条件。 You can apply the criteria in getTags() method in the Task entity. 您可以在Task实体的getTags()方法中应用条件。

public function getTags()
{
    return $this->tags->matching(Criteria::create()->where(Criteria::expr()->eq('archived',true)));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM