2012-09-18 211 views
0

我使用很多在我的Symfony2的項目很多關係,我已經成功地做了兩個實體「用戶」和Groups,我也成功地堅持其通過將數據插入usersgroupsusers_groups表數據多對多關係Symfony2的

$user = new User(); 
$user->getGroups()->add($group); 

現在我想修改也應編輯users_groups記錄的用戶..

我尋覓了很多,但沒有luck.Any幫助將不勝感激......

控制器

public function editAction() 
    { 
    if($this->getRequest()->query->get("id")){ 
     $id = $this->getRequest()->query->get("id"); 
     $request = $this->getRequest(); 

     $em = $this->getDoctrine()->getEntityManager(); 

     $entity = $em->getRepository('DesignAppBundle:Users')->find($id); 


     $editForm = $this->createForm(new UserType()); 


     if ($this->getRequest()->getMethod() == 'POST') { 
     $editForm->bindRequest($request); 
     if ($editForm->isValid()) { 

          $postData = $request->request->get('users'); 


          $repository = $this->getDoctrine() 
          ->getRepository('Bundle:groups'); 
          $group = $repository->findOneById($postData['group']); 

          $entity->addGroups($group); 

          $em->flush(); 

          return $this->redirect($this->generateUrl('index'));    
      } 

     } 

     return $this->render('User.html.twig',array(
      'entity'  => $entity, 
      'edit_form' => $editForm->createView(), 
     )); 
    }  
    } 

FORM

<?php 

use and include ..... 


class UserType extends AbstractType 
{ 
    public function buildForm(FormBuilder $builder, array $options) 
    { 
    $request = Request::createFromGlobals(); 

     $builder->add('group', 'entity', array(
     'class' => 'Bundle:Groups', 
     'property' => 'name', 
     'empty_value' => 'All', 
     'required' => true, 
      'multiple'=>true, 
     'data' => $request->get('group_id') 
     )); 



    } 

    public function getDefaultOptions(array $options) 
    { 

     return array(
      'validation_groups' => array('users'), 
      'csrf_protection' => false, 

     ); 
    } 

    public function getName() 
    { 
     return 'users'; 
    } 

} 

用戶

/** 
    * @ORM\ManyToMany(targetEntity="Groups", inversedBy="users") 
    * @ORM\JoinTable(name="users_groups", 
    *  joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}, 
    *  inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")} 
    *  ) 
    **/ 
    protected $group; 

public function __construct() 
{ 
    $this->group= new ArrayCollection();   

} 

    public function addgroups(\Design\AppBundle\Entity\Categories $group) 
    { 
     $this->group[] = $group; 
    } 

集團

/** 
    * @ORM\ManyToMany(targetEntity="Users", mappedBy="group") 
    */ 
    protected $users; 

    public function __construct() 
    { 
     $this->users= new ArrayCollection(); 
    } 

    /** 
    * Add users 
    * 
    * @param Bundle\Entity\Users $users 
    */ 
    public function addUsers(Bundle\Users $users) 
    { 
     $this->users[] = $users; 
    } 

    /** 
    * Get users 
    * 
    * @return Doctrine\Common\Collections\Collection 
    */ 
    public function getUsers() 
    { 
     return $this->users; 
    } 
+0

[whathaveyoutried.com](http://whathaveyoutried.com)? –

+0

感謝您的鏈接,但在此刻與我的問題有關的東西將不勝感激.... – user1577208

+0

那麼,你有什麼嘗試? –

回答

0

你不應該添加GROUP_ID,你應該加入組親perty

 $builder->add('groups'); 

group_id列自動由Doctrine處理,您不應該直接自己處理它。你有一個group_id屬性?如果你這樣做,你應該刪除它。

成組實體應該是:

/** 
* @ORM\ManyToMany(targetEntity="Users", mappedBy="group") 
*/ 
protected $users; 

public function __construct() 
{ 
    $this->users= new ArrayCollection(); 
} 

/** 
* Add users 
* 
* @param Bundle\Entity\User $user 
*/ 
public function addUsers(Bundle\User $users) 
{ 
    $this->users[] = $users; 
} 

/** 
* Get users 
* 
* @return Doctrine\Common\Collections\Collection 
*/ 
public function getUsers() 
{ 
    return $this->users; 
} 

編輯

這是你的控制器應如何:

public function editAction($request) 
{ 
    $id = $this->getRequest()->query->get("id"); 

    $em = $this->getDoctrine()->getEntityManager(); 

    $entity = $em->getRepository('DesignAppBundle:Users')->find($id); 

    $editForm = $this->createForm(new UserType(),$entity); 

    if ($this->getRequest()->getMethod() == 'POST') { 
     $editForm->bindRequest($request); 
     if ($editForm->isValid()) { 
      $em->persits($entity); 

      $em->flush(); 

      return $this->redirect($this->generateUrl('index'));    
     } 

    } 

    return $this->render('User.html.twig',array(
     'entity'  => $entity, 
     'edit_form' => $editForm->createView(), 
    )); 
} 

,這是你的用戶類型應該是這樣:

class UserType extends AbstractType 
{ 
    public function buildForm(FormBuilder $builder, array $options) 
    { 

     $builder->add('group', 'entity', array(
     'class' => 'Bundle:Groups', 
     'property' => 'name', 
     'empty_value' => 'All', 
     'required' => true, 
      'multiple'=>true, 
     )); 

    } 

    public function getDefaultOptions(array $options) 
    { 

     return array(
      'validation_groups' => array('users'), 
      'csrf_protection' => false, 

     ); 
    } 

    public function getName() 
    { 
     return 'users'; 
    } 

} 
+0

同樣的問題....它在user_group表中插入一個新條目,而不是更新前一個 – user1577208

+0

你可以發佈你的用戶和組實體嗎?只有關係 –

+0

關係的位請檢查更新.... – user1577208