如何在formtype symfony2中添加where子句

时间:2013-09-14 20:25:00

标签: forms symfony include

现在已经好几天了,我正在寻找这个问题的“方法”。我刚刚学习了symfony2,我无法找到解决方法,我尝试过在网上找到的不同内容,但我无法工作。

这是我的问题,我有一个表单PictureType,其中我包含另一个实体Collection(这是一个图片集合,或者如果您更喜欢图片专辑)。

当用户上传他的照片时,他需要选择他想要放入的相册。

我的问题是我无法弄清楚如何告诉我的PictureType只选择当前用户的集合。

这是我的pictureType

 $builder
        ->add('file')
        ->add('collection', 'entity', array(
            'class' => 'AppPictureBundle:Collection',
            'property' => 'name'
            ));

我想在属性之后插入这样的

其中'user_id'= $ this-> getUser()

我在Collection目标用户和图片目标集合上的ManyToOne上有很多ToOne。

1 个答案:

答案 0 :(得分:2)

那里有the query_builder option

$builder
    ->add('file')
    ->add('collection', 'entity', array(
        'class' => 'AppPictureBundle:Collection',
        'property' => 'name',
        'query_builder' => function (EntityRepository $er) {
            return $er->createQueryBuilder('c')
                ->where('c.user = :user')
                ->setParameter('user', $this->getCurrentUser());
        },
    ));