Note that there are some explanatory texts on larger screens.

plurals
  1. PORender a many-to-many relation with extra fields into a form
    primarykey
    data
    text
    <p>I'm wondering what is the best way to create a form that handle a many-to-many relation with extra field in symfony2.</p> <p>For the example let's say I would like to create a notifying feature for users. I have the <code>User</code> and the <code>Notification</code> entities. Also in order to be able to add extra parameters in the many-to-many relation between entities, I created a third entity <code>UserNotification</code>. The extra parameter indicates if the notification has been read or not by the users.</p> <pre><code>* @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; ... } </code></pre> <p>This way I am able to get this kind of data</p> <pre><code> 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 | +---------+-----------------+---------+ </code></pre> <p>Ok now here is the problem, how to make a form which allow to send a notification to some users? With a classic many-to-many relation it was not a problem. I had a <code>NotificationType</code> with the following <code>builder</code>:</p> <pre><code> $builder-&gt;add('users', 'entity', array( 'class' =&gt; 'User', 'property' =&gt; 'name', 'multiple' =&gt; 'true', )) -&gt;add('message', 'text'); </code></pre> <p>But since I had to create the intermediate <code>UserNotification</code> entity, I have no idea how to do it. Thanks for your help ;)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload