Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用Doctrine2更新关联实体?_Php_Doctrine_Symfony_Doctrine Orm_Entity Relationship - Fatal编程技术网

Php 如何使用Doctrine2更新关联实体?

Php 如何使用Doctrine2更新关联实体?,php,doctrine,symfony,doctrine-orm,entity-relationship,Php,Doctrine,Symfony,Doctrine Orm,Entity Relationship,相框与照片有单向的一对一关联 我想现在每一帧都和一张照片联系在一起了 我还猜想照片中没有关于相框的信息 我想知道哪张照片在相框中(有一张照片在相框中) 我不想知道照片在哪一帧 我假设我可以在一帧以上拍摄一张照片 到目前为止,一切都按预期进行: $photo = new Photo(); $another_photo = new Photo(); $frame = new Frame(); $frame->setPhoto($photo); $em->persist($frame

相框与照片有单向的一对一关联

  • 我想现在每一帧都和一张照片联系在一起了
  • 我还猜想照片中没有关于相框的信息
  • 我想知道哪张照片在相框中(有一张照片在相框中)
  • 我不想知道照片在哪一帧
  • 我假设我可以在一帧以上拍摄一张照片
到目前为止,一切都按预期进行:

$photo = new Photo();
$another_photo = new Photo();
$frame = new Frame();
$frame->setPhoto($photo);

$em->persist($frame);
$em->flush();
创建框架和两张照片,框架与数据库中的$photo连接。我可以使用getPhoto()查询$frame并获取$photo

但我现在想更新相框的照片,但它不起作用:

$frame = $em->getRepository('Frame')->findOneById(id_of_frame);
$frame->setPhoto($another_photo);
$em->persist($frame);
$em->flush();
并得到一个条令错误: 通过关系“Frame#Photo”找到了一个新实体,该关系未配置为级联实体的持久化操作。显式持久化新实体或在关系上配置级联持久化操作

我可以在Frame-OneToOne关系中设置一个cascade=“persist”,错误不再发生,但是我得到了一个关于我在照片中使用的未持久化颜色的新错误,我确实没有设置并且想要:(

为什么我会出现错误?为什么我需要保存帧和照片


谢谢!

您是如何创建$anothor\u照片的? 由于它已经在数据库中,您需要执行以下操作: $other\u photo=$entityManager->getReference('photo',$other\u photo\u id)


顺便说一句,整个级联过程可能会变得相当混乱。我想你会发现,在创建新照片时,只保留新照片更安全。

请添加你的条令映射配置。非常感谢getReference提示。我愚蠢到将现有对象设置为关系,条令假装这样做了我不知道。