gpt4 book ai didi

php - 更新双向多对多关系的正确方法 symfony2-doctrine

转载 作者:可可西里 更新时间:2023-10-31 22:12:48 24 4
gpt4 key购买 nike

我做了一些研究,在阅读之后thisthis (以及所有相关问题)我仍然无法确定在 Symonfy 2 Doctrine 中更新多对多关系的正确方法。感觉应该有很简单的方法,我还没找到。

我有这两个实体:

class student_main
{
/**
* @ORM\ManyToMany(targetEntity="support_log", inversedBy="student_main")
* @ORM\JoinTable(name="support_log_student")
**/
private $support_log;

class support_log
{
/**
* @ORM\ManyToMany(targetEntity="student_main", mappedBy="support_log")
**/
private $student;

我想从 support_log 开始。在 Controller 中,在更新操作中,我有类似的东西:

if ($editForm->isValid()) {        
//add the relationship the user added
foreach($students as $student){
if(!$em->getRepository('mybundle:student_main')->hasSupportLog($entity,$student)){
$entity->addstudent_main($student);//*
}
}
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('support_log_edit', array('id' => $id)));
}

当然,正如 doctrine Documentation 所说,我相应地更改了该函数 (addstudent_main):

public function addstudent_main(student_main $student)
{
$student->addsupport_log($this); // the important addition
$this->student[] = $student;
}

这很好用,我的问题更多是关于删除关系。在表单中有一个多选,用户可能会选择一些已经相关的学生和一些不相关的学生。感觉应该有一种自动的方式来做到这一点,但我不得不编写大量代码。

在 Controller 中,略高于我之前编写的代码,我将:

//delete all old relationship
foreach($idsldstudents as $idst){ //I take Id's because the doctrine collection is updating always..
$stu=$em->getRepository('MyBundle:student_main')->find($idst);
$stu->deletesupport_log($entity);//I had to create that method (in the entity, I do "$this->support_log->removeElement($support_log)")
$em->persist($stu);
$em->flush();
}

我删除了相关实体的所有关系(当然,注意是双向关系,所以必须先在另一边删除),然后添加用户选择的关系.

还有其他方法可以做到这一点,但我还没有找到任何简单的方法。在所有这些中,我都有同样的问题:

  • 我需要一直检查关系是否存在
  • 我需要获取旧关系(这很困难)并与用户指示的新关系进行比较,并相应地删除或创建

有没有一种方法可以自动解决这两个问题? (我有一种强烈的感觉,一定有 - 也许有更好的关系声明? - 这就是我问的原因)。

提前致谢

编辑:我的表单没有什么特别之处,我想我什至没有碰过生成的代码。它显示了我想要的多选,Symfony2 的默认设置,你必须使用 ctrl 键来选择多个。这是代码:

public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('student')
...
;
}

关键靠在这里?

最佳答案

到目前为止,(为了避免永远没有答案的问题),似乎没有“我还没有找到的简单方法”来做到这一点。根据评论,这就是我的问题的答案。

但由于上次评论的改进,代码可以改进并使其更加优雅。如果在实体级别,我们有:gist.github.com/3121916(来自评论)

那么,controller中的代码可以稍微精简一下:

$editForm->bindRequest($request);
if ($editForm->isValid()) {
//delete all old relationships, we can go from student:
foreach($em->getRepository('mybundle:student_main')->findAll() as $oldstudent)
{
$oldstudent->removeSupportLog($entity);
//if they are related, the relationship will be deleted.
//(check the code from the url)
}
//add the relationship the user added in the widget
$students=$entity->getStudent();
foreach($students as $student)
{
$entity->addstudent_main($student);
}
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('support_log_edit', array('id' => $id)));
}

它仍然不是我期望的“神奇”symfony 解决方案,但到目前为止我能做的最好的(也许将这段代码分组到存储库中的一个函数中,以使其更优雅)。

如果您有更好的想法,我会洗耳恭听。

关于php - 更新双向多对多关系的正确方法 symfony2-doctrine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15635599/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com