很多关系Symfony2

时间:2012-09-18 12:44:49

标签: symfony doctrine-orm

我在symfony2项目中使用了很多关系,我已经成功地创建了两个实体'Users'和Groups,并且我还成功地保存了将数据插入users,{{1}的数据使用

表和groups
users_groups

现在我想编辑也应该编辑users_groups记录的用户..

我搜索了很多但没有运气。任何帮助都会受到赞赏......

CONTROLLER

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

FORM

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

用户

<?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;
    }

1 个答案:

答案 0 :(得分:0)

您不应该添加group_id,您应该添加groups属性

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

这就是你的UserType应该是这样的:

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';
    }

}