[英]how to remove a unidirectional many to many association with Doctrine 2
I have a unidirectional many to many association between Badges and Request like so : 我在Badge和Request之间有一个单向的多对多关联,如下所示:
Badges: 徽章:
<?php
namespace Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Device
*
* @ORM\Table(name="badges")
* @ORM\Entity
*/
class Badges
{
/**
* Unidirectional - Many users have Many favorite comments (OWNING SIDE)
*
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Entity\Request", cascade={"persist"})
*/
private $invite;
}
Nothing special on the Request entity since this is a simple unidirectional association 因为这是一个简单的单向关联,所以对Request实体没什么特别的
Adding an association is fine. 添加关联就可以了。
But when removing the association I am doing this : 但是,当删除关联时,我正在这样做:
$em = $this->CI->doctrine->em;
//Get badges for new notifs
$badges = $user->getBadges();
if( $badges )
{
$invites = $badges->getInvite();
if ( $invites )
{
foreach ( $invites as $key => $invite )
{
$badges->removeInvite( $invite );
}
}
$em->persist( $badges );
$em->flush();
}
else
{
return false;
}
But it doesn't work and bring this error message for each invite I'm trying to detach from Badge: 但是它不起作用,并且在我尝试从徽章分离的每个邀请中都出现此错误消息:
A PHP Error was encountered 遇到PHP错误Severity: Notice 严重程度:注意
Message: Undefined index: 000000005ede1b52000000009e09e897 消息:未定义的索引:000000005ede1b52000000009e09e897
Filename: ORM/UnitOfWork.php 档名:ORM / UnitOfWork.php
Line Number: 2739 行号:2739
A PHP Error was encountered 遇到PHP错误Severity: Warning 严重程度:警告
Message: array_pop() expects parameter 1 to be array, null given 消息:array_pop()期望参数1为数组,给定null
Filename: Persisters/ManyToManyPersister.php 文件名:Persisters / ManyToManyPersister.php
Line Number: 143 行号:143
Now if I only remove one single request from Badge: 现在,如果我仅从“徽章”中删除一个请求:
$invites = $badges->getInvite();
if ( $invites )
{
foreach ( $invites as $key => $invite )
{
if( !empty( $invite ) ) {
$badges->removeInvite( $invite );
$em->persist( $invite );
$em->persist( $badges );
break;
}
}
}
I get : 我得到:
<b>Fatal error</b>: Uncaught exception 'Doctrine\\ORM\\ORMInvalidArgumentException' with message 'A new entity was found through the relationship 'Entity\\Badges#invite' that was not configured to cascade persist operations for entity: Entity\\Request@000000007c08aa8800000000c42081b4. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'Entity\\Request#__toString()' to get a clue.' in /var/www/meetmyfriends-dev/application/libraries/Doctrine/ORM/ORMInvalidArgumentException.php:59
Note that I have persisted Request as specified in this error message ( Entity\\Badges#invite is a Entity\\Request ) 请注意,我已按照此错误消息中的说明保留了请求(Entity \\ Badges#invite是Entity \\ Request)
Why is this happening ? 为什么会这样呢? What should I do to fix it ? 我该怎么解决? I just want to detach invite from Badges to remove to association. 我只想从“徽章”中分离邀请以将其移除以关联。
Thanks 谢谢
EDIT: This is the implementation of Badges#removeInvite Method & Badges#addInvite Method: 编辑:这是Badges#removeInvite方法和Badges#addInvite方法的实现:
/**
* Add invite
*
* @param \Entity\Request $invite
* @return Badges
*/
public function addInvite(\Entity\Request $invite)
{
$this->invite[] = $invite;
return $this;
}
/**
* Remove invite
*
* @param \Entity\Request $invite
*/
public function removeInvite(\Entity\Request $invite)
{
$this->invite->removeElement($invite);
}
Try this: 尝试这个:
foreach ( $invites as $key => $invite )
{
if(!empty($invite)){
$badges->removeInvite( $invite );
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.