Doctrine2:警告:isset中的偏移量类型为非法或为空

rva*_*iev 2 php doctrine symfony doctrine-orm

我在清单实体中有一个ManyToMany关系:

/**
 * @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;
    }
}
Run Code Online (Sandbox Code Playgroud)

在ListingService的某个地方,我试图从Listing实体中删除AttributeDescription,如下所示:

$listing->removeAttributeDescription($toRemoveAttributeDescription);
Run Code Online (Sandbox Code Playgroud)

但出现错误:警告:isset中的偏移量类型非法或为空

使用xdebug,我进入了ArrayCollection中的remove()方法:

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;
}
Run Code Online (Sandbox Code Playgroud)

并发现问题出在isset($ this-> elements [$ key])。这是xdebug的屏幕截图:

在此处输入图片说明

如您所见,$ key包含AttributeDescription,而$ this-> elements是AttributeDescriptions的数组。

我真的不知道我做错了什么还是这是一个理论错误?

我正在使用:Symfony 3.3.13与PHP 7.1.1

教义:

"doctrine/doctrine-bundle": "^1.6",
"doctrine/doctrine-cache-bundle": "^1.2",
"doctrine/doctrine-migrations-bundle": "^1.2",
"doctrine/orm": "^2.5"
Run Code Online (Sandbox Code Playgroud)

rva*_*iev 8

解决方法:我使用了错误的方法。而不是remove()我应该使用removeElement()