【问题标题】:Render a many-to-many relation with extra fields into a form将带有额外字段的多对多关系呈现到表单中
【发布时间】:2012-11-06 06:46:37
【问题描述】:

我想知道创建一个处理与 symfony2 中的额外字段的多对多关系的表单的最佳方法是什么。

例如,假设我想为用户创建一个通知功能。我有UserNotification 实体。另外为了能够在实体之间的多对多关系中添加额外的参数,我创建了第三个实体UserNotification。额外参数指示用户是否已阅读通知。

* @ORM\Entity() */
class Notification
{
    /** @Id @Column(type="integer") */
    private $id;

    /** @OneToMany(targetEntity="UserNotification", mappedBy="notification") */
    private $usernotifications;

    /** @Column() */
    private $message;

    ...
}

* @ORM\Entity() */
class User
{
    /** @Id @Column(type="integer") */
    private $id;

    /** @OneToMany(targetEntity="UserNotification", mappedBy="user") */
    private $usernotifications;

    /** @Column() */
    private $name;

    ...
}

* @ORM\Entity() */
class UserNotification
{
    /** @ManyToOne(targetEntity="User", inversedBy="usernotifications")
    private $user;

    /** @ManyToOne(targetEntity="Message", inversedBy="usernotifications")
    private $message;

    /** @Column(type="boolean") */
    private $isRead;

    ...
}

这样我就能得到这种数据

      User
+----+---------+
| id | name    |
+----+---------+
|  1 | John    |
|  2 | Paul    |
+----+---------+

      Notification
+----+----------------------+
| id | message              |
+----+----------------------+
|  1 | Cool stuff           |
|  2 | Lorem ipsum          |
+----+----------------------+

      UserNotification
+---------+-----------------+---------+
| user_id | notification_id | isRead  |
+---------+-----------------+---------+
|  1      |              1  |       1 |
|  2      |              1  |       0 |
|  1      |              2  |       0 |
|  2      |              2  |       1 | 
+---------+-----------------+---------+

好的,现在问题来了,如何制作一个允许向某些用户发送通知的表单?对于经典的多对多关系,这不是问题。我有一个NotificationType,下面是builder

    $builder->add('users', 'entity', array(
                'class' => 'User',
                'property' => 'name',
                'multiple' => 'true',
            ))
            ->add('message', 'text');

但由于我必须创建中间 UserNotification 实体,我不知道该怎么做。感谢您的帮助;)

【问题讨论】:

    标签: forms symfony doctrine-orm


    【解决方案1】:

    我也是这么做的。我的InternalMessage 实体是您的Notification。在表单中,users 字段未映射(property_pathfalse),并且已使用订阅者进行验证:

    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('title', 'text', array(
                'label' => 'Titolo *'
            ))
            ->add('content',  'textarea', array(
                'label' => 'Contenuto *'
            ))
            ->add('priority', new PriorityType(), array(
                'label' => 'Priorità *'
            ))
            ->add('users', 'entity', array(
                'label'         => 'Destinatari *',
                'class'         => 'Acme\HelloBundle\Entity\User',
                'property'      => 'select_label',
                'multiple'      => true,
                'expanded'      => true,
                'property_path' => false,
            ));
        ;
    
        $builder->addEventSubscriber(new AddUsersValidationSubscriber());
    }
    

    在我的控制器中,我唯一要做的事情(假设表单有效)是为表单模型中的每个用户创建一个 InternalMessageFeedback 实体(与您的 UserNotification 相同):

    $message = new InternalMessage();
    $form    = $this->createForm(new InternalMessageType(), $message);
    
    // ...
    
    // The sender
    $message->setUser($this->getSecurityContext()->getToken()->getUser());
    
    // Persist the inverse side
    $em = $this->getEntityManager();
    $em->persist($message);
    
    // One feedback for each user
    /** @var $user \Acme\HelloBundle\Entity\User */
    foreach($form->get('users')->getData() as $user) {
        $feedback = new InternalMessageFeedback();
    
        // Se the message and user for current feedback
        $feedback->setInternalMessage($message);
        $feedback->setUser($user);
    
        // Persist the owning side
        $em->persist($feedback);
    
        // Sync the inverse side
        $message->addInternalMessageFeedback($feedback);
    }
    
    $em->flush();
    

    希望这会有所帮助:)

    【讨论】:

    • 感谢您的回答 Gremo ;) 我不知道 property_path 属性。它的作用就像一个魅力。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 2012-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多