简体   繁体   English

Symfony2表单集合-

[英]Symfony2 form collection -

I am building a gallery of images which must allow for tagging each image with keywords. 我正在建立图片库,该图片库必须允许使用关键字标记每个图片。 To handle the tags, I'm using FPN/TagBundle ( https://github.com/FabienPennequin/FPNTagBundle ). 为了处理标签,我使用了FPN / TagBundle( https://github.com/FabienPennequin/FPNTagBundle )。

I've already built the form, using the following: 我已经使用以下命令构建了表单:

// UserAlbumImageType.php

...
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('description', null, array('label' => 'Description'))
        //TODO: add tags
        ->add('tags', null, array(
            'label' => 'Tags',
            'mapped' => false,
            'required' => false,
            'attr' => array(
                'class' => 'tags',
            ),
        ))
        ->add('licenseType', 'entity', array(
            'label' => 'License',
            'class' => 'VoxCoreBundle:LicenseType',
        ))
        ->add('privacyType', null, array('label' => 'Privacy'))
        ;
}$builder
                ->add('images', 'collection', array(
                    'type' => new UserAlbumImageType(),
                    'label' => false,
                ))
            ;
            break;
...

// UserAlbumType.php

...
$builder
    ->add('images', 'collection', array(
        'type' => new UserAlbumImageType(),
        'label' => false,
    ))
    ;
    break;
...

As you can see, the tags property is NOT mapped. 如您所见,tags属性未映射。 This is because I don't want to write the tags into a field in the database, but instead persist them to a central tag table. 这是因为我不想将标签写入数据库的字段中,而是将它们持久保存到中央标签表中。 And that's where the problem lies. 这就是问题所在。

When the form is submitted, I'm simply calling $em->persist($userAlbum) which then persists changes to the UserAlbumImage objects in the collection. 提交表单后,我只是调用$em->persist($userAlbum) ,然后将更改UserAlbumImage化对集合中UserAlbumImage对象的更改。 At this time, I'd like to grab the tags that were submitted via the form, and set them using the tag manager. 目前,我想获取通过表单提交的标签,并使用标签管理器进行设置。 I'm unsure where to handle this. 我不确定在哪里处理。 In a Doctrine postPersist listener? 在学说后坚持听众? If so, I'll still need to save the tags to the entity at least temporarily, then parse them. 如果是这样,我仍然需要至少暂时将标签保存到实体,然后解析它们。 Is there a better way? 有没有更好的办法?

Why not in your controller : 为什么不在您的控制器中:

// ...
$tags = $form->getData()->getTags();
foreach($tags as $tag) {
    $em->persist($tag);
}
// ...

$em->flush();

If I were you, I'd follow (as I'm always trying to do) the MVC pattern with added repositories. 如果我是您,那么我将遵循(因为我一直在尝试做)添加存储库的MVC模式。 I'd implement a saveGallery method in the repository for the gallery entity. 我会在图库实体的存储库中实现saveGallery方法。 This would get called from the controller (similar as mansolux recommended, but instead having the store functionality in the controller (bad practice, if you ask me), call the repository method for it). 这可以从控制器中调用(与推荐的mansolux类似,但是要在控制器中具有存储功能(不好的做法,如果您问我),请为此调用存储库方法)。 The method would receive all the submitted data. 该方法将接收所有提交的数据。 It would first store all the entities that need to be stored before the tags (gallery, images, whatnot). 它会首先存储所有需要存储在标签之前的实体(图库,图像等)。 After that I'd get the tag repository: 之后,我将获得标签存储库:

$repo = $this->em->getRepository("FPNTagBundle:TagEntityName");

Now, the only thing left to do is to store the tags using this repository. 现在,剩下要做的就是使用此存储库存储标签。 You can add some sanity checks to make sure the tag bundle you're using actually exists, but that's something for you to decide. 您可以添加一些健全性检查,以确保您正在使用的标签包确实存在,但这是您要决定的事情。

Hope it helps. 希望能帮助到你。

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

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