[英]Symfony2 ManyToMany with attributes
I'm trying to manage a manyToMany relation with attributes in one view for delete/update/add. 我正在尝试在一个视图中使用删除/更新/添加属性来管理多对多关系。
This is my relation : a User can speak some langages with a level. 这是我的关系:用户可以说一些带有语言的语言。 So There are a table for Users, Langages and the third for manage the relation with the level. 因此,有一个表供用户,语言使用,而第三个表用于管理与级别的关系。
This is my controller : 这是我的控制器:
public function gererLangueAction(Request $request, User $user){
$form = $this->createForm(new ajouterLangueFormType, $user);
if ($request->getMethod() == 'POST') {
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
if(!empty($request->request->get("intranet_rh_gererlangue")['userLangues']))
foreach($request->request->get("intranet_rh_gererlangue")['userLangues'] as $langue)
{
$l = $em->getRepository('IntranetRhBundle:Langue')->findOneById($langue['langue']);
$userLangues = new UserLangue();
$userLangues->setUser($user);
$userLangues->setLangue($l);
$userLangues->setNiveau($langue['niveau']);
$user->addUserLangue($userLangues);
$em->persist($userLangues);
}
$em->flush();
$this->get('session')->getFlashBag()->add('success', "Les langues de ". $user->getNom() ." ". $user->getPrenom() . " ont été modifiées avec succès !");
return $this->redirect($this->generateUrl('intranet_rh_homepage'));
}else
$this->get('session')->getFlashBag()->add('danger', "Erreur de formulaire !");
}
return $this->render('IntranetRhBundle:User:gererLangue.html.twig',array('user' => $user, 'form' => $form->createView()));
}
My view is ok, my JS allow me to add and delete and I can update and delete some languages if they already exist. 我的观点是正确的,我的JS允许我添加和删除,并且可以更新和删除一些已经存在的语言。
But when I want to add a language, I have this message : 但是当我想添加一种语言时,我会收到以下消息:
"Entity of type Intranet\\RhBundle\\Entity\\UserLangue is missing an assigned ID for field 'user'. The identifier generation strategy for this entity requires the ID field to be populated before EntityManager#persist() is called. If you want automatically generated identifiers instead you need to adjust the metadata mapping accordingly. " “类型为Intranet \\ RhBundle \\ Entity \\ UserLangue的实体缺少为字段'user'分配的ID。此实体的标识符生成策略要求在调用EntityManager#persist()之前填充ID字段。如果要自动生成标识符,您需要相应地调整元数据映射。”
But I don't understand why because of my doctrine annotation on each class : 但是我不明白为什么,因为我在每堂课上都用我的学说注解:
for UserLangue : 对于UserLangue:
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Intranet\UserBundle\Entity\User", inversedBy="userLangues")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)
*/
private $user;
For User : 对于用户:
/**
* @ORM\OneToMany(targetEntity="Intranet\RhBundle\Entity\UserLangue", mappedBy="user", cascade={"persist", "remove"}, orphanRemoval=true)
**/
private $userLangues;
With adding and removing method. 具有添加和删除方法。 I'm working on this bug since 3 days but nobody already had a similar mistake... 自3天以来我一直在研究此错误,但没有人遇到类似的错误...
You can try to add @ORM\\GeneratedValue(strategy="AUTO")
in the annotation, but having a relation as the Id is not a very good ideea and I don't think it can even work. 您可以尝试在@ORM\\GeneratedValue(strategy="AUTO")
中添加@ORM\\GeneratedValue(strategy="AUTO")
,但是将关系作为ID并不是一个很好的想法,我认为它甚至都无法工作。 Try adding an $id parameter and set the relation as a different parameter. 尝试添加$ id参数并将关系设置为其他参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.