正确的方式更新双向许多关系symfony2-doctrine [英] Proper way to update bidirectional Many to Many relationship symfony2-doctrine

查看:154
本文介绍了正确的方式更新双向许多关系symfony2-doctrine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一些研究,看完 (和所有相关的问题)我仍然无法确定哪个是更新Symonfy 2学说中许多关系的正确方法。觉得应该有一个非常简单的方法,我还没有找到。

I did some research, and after reading this and this (and all the related questions) I still can't figure which is the proper way to update a many to many relationship in Symonfy 2 Doctrine. It feels that there should be a very simple way of doing it that I still haven't found.

我有这2个实体:

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 开始。在控制器中,在更新操作中,我有这样的东西:

I want to start from support_log. In the controller, in the update action, I have something like that:

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

当然,正如教义文档所说,我相应地改变了这个函数(addstudent_main) :

Of course, as doctrine Documentation says, I changed that function (addstudent_main) accordingly:

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

这工作正常,我的问题更多的是删除关系。在形式上有多重选择,用户可能会选择一些已经相关的学生,而有些学生则没有。感觉应该有一种自动的方法,但是我不得不做很多代码。

This works fine, my question is more about deleting the relationship. In the form there is a multiselect, and the user might select some students that are already related and some that don't. It feels that there should be an automatic way of doing that, but instead I had to do lots of code.

在控制器中,稍微高于我之前写的代码我把它放在那里:

In the controller, just slightly above the code I wrote before, I put that:

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

我删除了有关实体的所有关系(当然,关心是双向关系,所以必须先在另一方面删除),然后用户选择的内容将被添加。

I delete all the relationships of the entity in question (of course, taking in care that is a bidirectional relationship, so it has to be deleted first in the other side), and then the ones that the user selected will be added.

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

There are other ways of doing that, but I haven't found any simple one. In all of them I have the same problems:


  • 如果关系存在,我需要检查所有的时间

  • 我需要获得旧的关系(这很困难),并与用户指出的新关系进行比较,并删除或创建相应的

有没有办法自动处理这两个问题? (我有一种强烈的感觉,一定是 - 可能会更好地宣布关系? - 这就是为什么我问)。

Is there a way of doing that that takes care of these 2 problems automatically? (I have a strong feeling that there must be - maybe with a better declaration of the relationship? - that's why I am asking).

提前感谢

编辑:
我的表单没什么特别之处,我想我甚至没有触及生成的代码。它显示我想要的多重选择,默认的Symfony2,您必须使用 ctrl 键来选择多个。这是代码:

My form hasn't anything special, I think I didn't even touch the generated code. It displays the multiselect that I want, the default of Symfony2, where you have to use ctrl key to select more than one. Here is the code:

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

密钥依赖于这里?

推荐答案

根据教义文件,只会检查协会的拥有方面是否有变更。

According to doctrine documentation it will only check the owning side of an association for changes.

http://doctrine-orm.readthedocs.org/en/latest/reference/unitofwork -associations.html

所以最好的办法是保留更新的边界实体的含义。

So the best way is keep owning side entity assotiations updated.

在这种情况下,您必须在添加和删除方法中删除并添加student_log中的support_log

In this case you must remove and add support_log in student main in add and remove methods

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

public function addStudent($student) {
  $this->student[] = $student;
  $student->addSupportLog($this);
}

public function removeStudent($student) {
  $student->removeSupportLog($this);
  $this->student->removeElement($student);
}

这些都不需要修改控制器的操作。
重要的是在协会的相反方面实现这一点!

Thats all no need to modify controller actions. Important implement this at the inverse side of association!

这篇关于正确的方式更新双向许多关系symfony2-doctrine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆