2012-11-17 44 views
9

Tôi tự hỏi cách tốt nhất để tạo biểu mẫu xử lý quan hệ nhiều-nhiều với trường bổ sung trong symfony2 là gì.Hiển thị mối quan hệ nhiều-nhiều với các trường bổ sung thành một biểu mẫu

Ví dụ: giả sử tôi muốn tạo tính năng thông báo cho người dùng. Tôi có User và các đối tượng Notification. Ngoài ra để có thể thêm các thông số bổ sung trong quan hệ nhiều-nhiều giữa các thực thể, tôi đã tạo một thực thể thứ ba UserNotification. Tham số bổ sung cho biết liệu thông báo đã được đọc hay không bởi người dùng.

* @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; 

    ... 
} 

Bằng cách này tôi có thể có được loại dữ liệu này

 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 | 
+---------+-----------------+---------+ 

Ok bây giờ ở đây là vấn đề, làm thế nào để thực hiện một hình thức cho phép để gửi một thông báo cho một số người dùng? Với một mối quan hệ nhiều-nhiều-cổ điển, nó không phải là một vấn đề. Tôi đã có một NotificationType với builder sau:

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

Nhưng kể từ khi tôi đã phải tạo ra các trung gian UserNotification tổ chức nào, tôi không có ý tưởng làm thế nào để làm điều đó. Cảm ơn sự giúp đỡ của bạn;)

Trả lời

4

Tôi đã làm chính xác như vậy. Pháp nhân InternalMessage của tôi là Notification của bạn. Trong hình thức, users lĩnh vực không được ánh xạ (property_pathfalse) và nó xác nhận sử dụng một thuê bao:

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()); 
} 

Trong thời gian ở bộ điều khiển của tôi, điều duy nhất tôi phải làm (giả dạng đó là hợp lệ) là để tạo ra một InternalMessageFeedback thực thể (giống như UserNotification của bạn) fore mỗi người dùng trong mô hình có dạng:

$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(); 

Hope this helps :)

+0

Cám ơn y Câu trả lời của chúng tôi Gremo;) Tôi không biết về thuộc tính 'property_path'. Và nó hoạt động như một sự quyến rũ. –

Các vấn đề liên quan