php - Doctrine 2 : Warning: Illegal offset type in isset or empty

标签 php symfony doctrine-orm doctrine

我在 Listing 实体中有一个 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;
    }
}

在 ListingService 的某个地方,我试图从 Listing 实体中删除 AttributeDescription,如下所示:
$listing->removeAttributeDescription($toRemoveAttributeDescription);

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

使用 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;
}

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

enter image description here

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

我真的不明白我是做错了什么还是这是一个 Doctrine 错误?

我正在使用:
Symfony 3.3.13 与 php 7.1.1

Doctrine :
"doctrine/doctrine-bundle": "^1.6",
"doctrine/doctrine-cache-bundle": "^1.2",
"doctrine/doctrine-migrations-bundle": "^1.2",
"doctrine/orm": "^2.5"

最佳答案

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

关于php - Doctrine 2 : Warning: Illegal offset type in isset or empty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47445439/

相关文章:

php - 从文本单元格到 "real"php 数组

Symfony JWT token : exception when token is expired

css - Symfony2 Assetic 在 css/less 文件中错误的 cssrewrite 资源路径

datetime - 切换日期区域设置

php - 如何在 doctrine2 中找到实体

mysql - 查找重叠或冲突的时间间隔

php - 从 SELECT WHERE IN 查询 MySQL 中提取值

php - 如何设计顺序哈希类函数

symfony - 使用 Doctrine 设置数据库连接超时

php - Laravel 4 迁移