1

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

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

当用户上传他的照片时,他需要选择他想把它放在哪个相册中。

我的问题是我不知道如何在我的 PictureType 中告诉只选择当前用户的集合。

这是我的图片类型

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

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

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

我在集合目标用户上有一个 manyToOne,在图片目标集合上有一个 ManyToOne。

4

1 回答 1

2

一个query_builder选项

$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());
        },
    ));
于 2013-09-14T20:26:11.463 回答