【问题标题】:Doctrine2: Warning: Illegal offset type in isset or emptyDoctrine2:警告:isset 中的非法偏移类型或为空
【发布时间】:2017-11-22 23:34:38
【问题描述】:

我在列表实体中有一个多对多关系:

/**
 * @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 中的偏移类型非法或为空

使用 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 的屏幕截图:

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

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

我正在使用: 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"

【问题讨论】:

    标签: php symfony doctrine-orm doctrine


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-21
      • 1970-01-01
      • 1970-01-01
      • 2019-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多