加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 17:44:39 所属栏目:PHP教程 来源:网络整理
导读:我做了一些研究,在阅读了 this和 this(以及所有相关问题)后,我仍然无法确定在Symonfy 2 Doctrine中更新多对多关系的正确方法.我觉得应该有一种非常简单的方法,我仍然没有找到它. 我有这2个实体: class student_main{/*** @ORMManyToMany(targetEntity="sup
我做了一些研究,在阅读了 this和 this(以及所有相关问题)后,我仍然无法确定在Symonfy 2 Doctrine中更新多对多关系的正确方法.我觉得应该有一种非常简单的方法,我仍然没有找到它.

我有这2个实体:

class student_main
{
/**
* @ORMManyToMany(targetEntity="support_log",inversedBy="student_main")
* @ORMJoinTable(name="support_log_student")
**/
private $support_log;

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

我想从support_log开始.在控制器中,在更新操作中,我有类似的东西:

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

这很好用,我的问题更多的是删除关系.在表单中有一个多选,用户可能会选择一些已经相关的学生和一些不相关的学生.感觉应该有一种自动的方法,但我必须做很多代码.

在控制器中,略高于我之前编写的代码,我把它说:

//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(来自评论)

然后,控制器中的代码可以减少一点:

$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解决方案,但到目前为止我能做的最好(可能将此代码组合在存储库中的一个函数中,使其更加优雅).

如果你有更好的想法,我会全力以赴.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读