Symfony 3,ArrayCollection 的 remove() 导致错误 "Warning: Illegal offset type in isset or empty"
Symfony 3, ArrayCollection's remove() causes error of "Warning: Illegal offset type in isset or empty"
我有一个 Wishlist 实体,它与使用 MTM Doctrine 注释的 Product 实体有关系。
我的定义是 $products
是 Wishlist 的 __construct() 中的 Array Collection
,这就是为什么我有 addProduct()
和 removeProduct()
方法。
因此,class 具有以下视图:
<?php
namespace WishlistBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use ShopBundle\Entity\Product;
/**
* Wishlist
*
* @ORM\Table(name="wishlist")
* @ORM\Entity()
*/
class Wishlist
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToMany(targetEntity="ShopBundle\Entity\Product")
* @ORM\JoinTable(
* name="mtm_products_in_wishlists",
* joinColumns={
* @ORM\JoinColumn(
* name="wishlist_id",
* referencedColumnName="id"
* )
* },
* inverseJoinColumns={
* @ORM\JoinColumn(
* name="product_id",
* referencedColumnName="id",
* unique=true
* )
* }
* )
*/
private $products;
...
/**
* @param Product $product
*/
public function addProduct(Product $product)
{
$this->products->add($product);
}
/**
* @param Product $product
*/
public function removeProduct(Product $product)
{
$this->products->remove($product);
}
/**
* Get products.
*
* @return string
*/
public function getProducts()
{
return $this->products;
}
/**
* Wishlist constructor.
*/
public function __construct()
{
$this->products = new ArrayCollection();
}
}
在我的控制器中,我有一个地方尝试使用 removeProduct()
方法。我按以下方式使用它:
$wishlist->removeProduct($product);
但是我收到以下错误:
Warning: Illegal offset type in isset or empty (500 Internal Server Error)
在
中的那一行
vendor\doctrine\collections\lib\Doctrine\Common\Collections\ArrayCollection.php at line 126
它有以下视图:
public function remove($key)
{
if ( ! isset($this->elements[$key]) && ! array_key_exists($key, $this->elements)) {
return null;
}
}
同时,addProduct()
工作正常。
我做错了什么?如何解决这个问题?
您要查找的是 ArrayCollection 的 removeElement($element)
函数,而不是 remove($key)
函数。
如其定义所示,remove($key)
函数从集合中删除指定索引 ($key) 处的元素,而 removeElement($element)
从集合中删除指定元素(如果找到)。
由于您尝试将产品作为元素而不是其索引删除,因此您应该使用 removeElement($product)
.
Doctrine ArrayCollection API 参考 here
注意: 您正在使用 ArrayCollection
。
/**
* @param Product $product
*/
public function addProduct(Product $product)
{
$this->products->add($product);
}
/**
* @param Product $product
*/
public function removeProduct(Product $product)
{
$this->products->removeElement($product);
}
我有一个 Wishlist 实体,它与使用 MTM Doctrine 注释的 Product 实体有关系。
我的定义是 $products
是 Wishlist 的 __construct() 中的 Array Collection
,这就是为什么我有 addProduct()
和 removeProduct()
方法。
因此,class 具有以下视图:
<?php
namespace WishlistBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use ShopBundle\Entity\Product;
/**
* Wishlist
*
* @ORM\Table(name="wishlist")
* @ORM\Entity()
*/
class Wishlist
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToMany(targetEntity="ShopBundle\Entity\Product")
* @ORM\JoinTable(
* name="mtm_products_in_wishlists",
* joinColumns={
* @ORM\JoinColumn(
* name="wishlist_id",
* referencedColumnName="id"
* )
* },
* inverseJoinColumns={
* @ORM\JoinColumn(
* name="product_id",
* referencedColumnName="id",
* unique=true
* )
* }
* )
*/
private $products;
...
/**
* @param Product $product
*/
public function addProduct(Product $product)
{
$this->products->add($product);
}
/**
* @param Product $product
*/
public function removeProduct(Product $product)
{
$this->products->remove($product);
}
/**
* Get products.
*
* @return string
*/
public function getProducts()
{
return $this->products;
}
/**
* Wishlist constructor.
*/
public function __construct()
{
$this->products = new ArrayCollection();
}
}
在我的控制器中,我有一个地方尝试使用 removeProduct()
方法。我按以下方式使用它:
$wishlist->removeProduct($product);
但是我收到以下错误:
Warning: Illegal offset type in isset or empty (500 Internal Server Error)
在
中的那一行vendor\doctrine\collections\lib\Doctrine\Common\Collections\ArrayCollection.php at line 126
它有以下视图:
public function remove($key)
{
if ( ! isset($this->elements[$key]) && ! array_key_exists($key, $this->elements)) {
return null;
}
}
同时,addProduct()
工作正常。
我做错了什么?如何解决这个问题?
您要查找的是 ArrayCollection 的 removeElement($element)
函数,而不是 remove($key)
函数。
如其定义所示,remove($key)
函数从集合中删除指定索引 ($key) 处的元素,而 removeElement($element)
从集合中删除指定元素(如果找到)。
由于您尝试将产品作为元素而不是其索引删除,因此您应该使用 removeElement($product)
.
Doctrine ArrayCollection API 参考 here
注意: 您正在使用 ArrayCollection
。
/**
* @param Product $product
*/
public function addProduct(Product $product)
{
$this->products->add($product);
}
/**
* @param Product $product
*/
public function removeProduct(Product $product)
{
$this->products->removeElement($product);
}