我在 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 的截图:
如您所见,$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/