Symfony2:如何通过某个属性过滤实体选择表单字段的选项?

时间:2022-10-16 18:06:26

1.) The Situation (simplified)

1.)情况(简化)

I have two entities: a Container-entity, which has exactly 1 Content-entity. The content_id is stored in the Container-entity.

我有两个实体:一个Container-entity,它只有一个Content-entity。 content_id存储在Container-entity中。

2.) Soft-Delete Content-entities

2.)软删除内容实体

I implemented a function to soft-delete Content-entities, so I added a "deleted"-attribute to the Content-entity. Everything works fine.

我实现了一个软删除内容实体的功能,所以我在Content-entity中添加了一个“已删除”的属性。一切正常。

3.) The problem

3.)问题

Now, when I want to create a new Container-entity, the auto-generated choices show ALL the Content-entities - even those ones, which I "marked as deleted" (delete-attribute = 1).

现在,当我想创建一个新的Container实体时,自动生成的选项显示所有内容实体 - 甚至是那些我“标记为已删除”的实体(delete-attribute = 1)。

4.) The question

4.)问题

Where is the correct place to add a "filter"/"query" to only show the elements which are not marked as deleted? (delete != 1)

添加“过滤器”/“查询”的正确位置在哪里才能显示未标记为已删除的元素? (删除!= 1)

5.) What I've tried

5.)我尝试过的

a.) view/twig approach: I tried to modify the rendering of the {{ form_widget(form.contentId) }} with no success

a。)view / twig方法:我尝试修改{{form_widget(form.contentId)}}的呈现但没有成功

b.) controller approach: I tried to manipulate the form-data in the newAction where the form is being created ($form = $this->createCreateForm($entity)) with no success

b。)控制器方法:我试图在创建表单的newAction中操作表单数据($ form = $ this-> createCreateForm($ entity))但没有成功

c.) type/buildForm approach: I tried to change the buildForm()-method ... again, no success

c。)type / buildForm方法:我试图改变buildForm() - 方法......再次,没有成功

If would be GREAT if you could give me a hint and/or a short code example of where I could hook into the action to remove the soft-deleted choices.

如果你能给我一个提示和/或一个简短的代码示例,我可以在哪里勾选到删除软删除选项的动作。

Thank you very much in advance!

非常感谢你提前!

1 个解决方案

#1


19  

You're looking for the query_builder option of the entity field.

您正在寻找实体字段的query_builder选项。

You can create a custom query that filters the resultset in there.

您可以创建一个自定义查询来过滤结果集。

example:

$builder->add('users', 'entity', array(
    'class' => 'AcmeHelloBundle:User',
    'query_builder' => function(EntityRepository $repository) {
        $qb = $repository->createQueryBuilder('u');
        // the function returns a QueryBuilder object
        return $qb
            // find all users where 'deleted' is NOT '1'
            ->where($qb->expr()->neq('u.deleted', '?1'))
            ->setParameter('1', '1')
            ->orderBy('u.username', 'ASC')
        ;
    },
));

You could go for a more general approach that filters all select statements using doctrine filters, too.

您可以采用更通用的方法,使用doctrine过滤器过滤所有select语句。

#1


19  

You're looking for the query_builder option of the entity field.

您正在寻找实体字段的query_builder选项。

You can create a custom query that filters the resultset in there.

您可以创建一个自定义查询来过滤结果集。

example:

$builder->add('users', 'entity', array(
    'class' => 'AcmeHelloBundle:User',
    'query_builder' => function(EntityRepository $repository) {
        $qb = $repository->createQueryBuilder('u');
        // the function returns a QueryBuilder object
        return $qb
            // find all users where 'deleted' is NOT '1'
            ->where($qb->expr()->neq('u.deleted', '?1'))
            ->setParameter('1', '1')
            ->orderBy('u.username', 'ASC')
        ;
    },
));

You could go for a more general approach that filters all select statements using doctrine filters, too.

您可以采用更通用的方法,使用doctrine过滤器过滤所有select语句。