我在我的symfony2项目中使用了多对多关系,我成功地创建了两个实体"Users"和Groups
,我还成功地持久化了数据,通过将数据插入到users
、groups
和users_groups
表中
$user = new User();
$user->getGroups()->add($group);
现在我想编辑用户,该用户还应该编辑usersgroups记录。。
我找了很多,但没有找到。任何帮助都将不胜感激。。。
控制器
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';
}
}
用户
/**
* @ORMManyToMany(targetEntity="Groups", inversedBy="users")
* @ORMJoinTable(name="users_groups",
* joinColumns={@ORMJoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORMJoinColumn(name="group_id", referencedColumnName="id")}
* )
**/
protected $group;
public function __construct()
{
$this->group= new ArrayCollection();
}
public function addgroups(DesignAppBundleEntityCategories $group)
{
$this->group[] = $group;
}
组
/**
* @ORMManyToMany(targetEntity="Users", mappedBy="group")
*/
protected $users;
public function __construct()
{
$this->users= new ArrayCollection();
}
/**
* Add users
*
* @param BundleEntityUsers $users
*/
public function addUsers(BundleUsers $users)
{
$this->users[] = $users;
}
/**
* Get users
*
* @return DoctrineCommonCollectionsCollection
*/
public function getUsers()
{
return $this->users;
}
您不应该添加group_id,而应该添加groups属性
$builder->add('groups');
group_id列由Doctrine自动处理,您不应该自己直接处理它。您有group_id属性吗?如果你这样做了,你应该删除它。
集团实体应为:
/**
* @ORMManyToMany(targetEntity="Users", mappedBy="group")
*/
protected $users;
public function __construct()
{
$this->users= new ArrayCollection();
}
/**
* Add users
*
* @param BundleEntityUser $user
*/
public function addUsers(BundleUser $users)
{
$this->users[] = $users;
}
/**
* Get users
*
* @return DoctrineCommonCollectionsCollection
*/
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';
}
}