Php Doctrine2:警告:isset中的偏移量类型非法或为空
我在上市实体中有很多关系:Php Doctrine2:警告:isset中的偏移量类型非法或为空,php,symfony,doctrine-orm,doctrine,Php,Symfony,Doctrine Orm,Doctrine,我在上市实体中有很多关系: /** * @ORM\Entity(repositoryClass="AppBundle\Repository\ListingRepository") * @ORM\Table(name="listings") */ class Listing extends MainEntity { /** * @ORM\Id * @ORM\Column(type="uuid") */ private $id; /**
/**
* @ORM\Entity(repositoryClass="AppBundle\Repository\ListingRepository")
* @ORM\Table(name="listings")
*/
class Listing extends MainEntity
{
/**
* @ORM\Id
* @ORM\Column(type="uuid")
*/
private $id;
/**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\AttributeDescription", inversedBy="listings", cascade={"persist", "remove"})
* @JoinTable(name="listing_attriubute_descriptions",
* joinColumns={@JoinColumn(name="listing_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="attribute_description_id", referencedColumnName="id")}
* )
*/
public $attributeDescriptions;
public function __construct()
{
$this->id = Uuid::uuid4();
$this->attributeDescriptions = new ArrayCollection();
}
public function removeAttributeDescription(AttributeDescription $attributeDescription)
{
if(!$this->attributeDescriptions->contains($attributeDescription))
{
return;
}
$this->attributeDescriptions->remove($attributeDescription);
return $this;
}
}
在ListingService的某个地方,我试图从列表实体中删除AttributeDescription,如下所示:
$listing->removeAttributeDescription($toRemoveAttributeDescription);
但出现错误:警告:isset中的偏移量类型非法或为空
在ArrayCollection中使用我登陆的xdebug删除()方法:
public function remove($key)
{
if ( ! isset($this->elements[$key]) && ! array_key_exists($key, $this->elements)) {
return null;
}
$removed = $this->elements[$key];
unset($this->elements[$key]);
return $removed;
}
并发现问题来自isset($this->elements[$key])。
以下是xdebug的屏幕截图:
正如您所看到的,$key包含一个AttributeDescription,$this->elements是一个AttributeDescription数组
我真的不知道我是做错了什么,还是这是一个条令错误
我正在使用:
使用PHP7.1.1的Symfony 3.3.13
原则:
"doctrine/doctrine-bundle": "^1.6",
"doctrine/doctrine-cache-bundle": "^1.2",
"doctrine/doctrine-migrations-bundle": "^1.2",
"doctrine/orm": "^2.5"
解决方法:我使用了错误的方法。我应该使用removeElement()而不是remove()