我在徽章和请求之间有多对多的单向关联,如下所示:
徽章:
<?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;
}
Request实体没什么特别之处,因为这是一个简单的单向关联
添加关联很好。
但是在删除关联时我正在这样做:
$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;
}
但它不起作用并为我试图从徽章中分离的每个邀请带来此错误消息:
遇到PHP错误
严重性:通知
消息:未定义的索引: 000000005ede1b52000000009e09e897
文件名: ORM / UnitOfWork.php
行号:2739
遇到PHP错误
严重性:警告
消息:array_pop()需要参数1 是数组,null给定
文件名: Persisters / ManyToManyPersister.php
行号:143
现在,如果我只从Badge中删除一个请求:
$invites = $badges->getInvite();
if ( $invites )
{
foreach ( $invites as $key => $invite )
{
if( !empty( $invite ) ) {
$badges->removeInvite( $invite );
$em->persist( $invite );
$em->persist( $badges );
break;
}
}
}
我明白了:
<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
请注意,我已在此错误消息中指定了持久请求(实体\徽章#custom是实体\请求)
为什么会这样? 我该怎么做才能解决这个问题?我只是想从徽章中删除邀请以删除协会。
由于
编辑: 这是徽章的实现#removeInvite Method&amp;徽章#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);
}
答案 0 :(得分:0)
试试这个:
foreach ( $invites as $key => $invite )
{
if(!empty($invite)){
$badges->removeInvite( $invite );
}
}