【问题标题】:Symfony 3, ArrayCollection's remove() causes error of "Warning: Illegal offset type in isset or empty"Symfony 3,ArrayCollection 的 remove() 导致错误“警告:isset 中的非法偏移类型或为空”
【发布时间】:2018-09-18 22:38:45
【问题描述】:

我有一个 Wishlist 实体,它与使用 MTM Doctrine 注释的 Product 实体有关系。 我的定义是 $products 在 Wishlist 的 __construct() 中是 Array Collection,这就是为什么我有 addProduct()removeProduct() 方法。 因此,该类具有以下视图:

<?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);

但我收到以下错误:

警告:isset 中的偏移类型非法或为空(500 内部服务器错误)

它在

中的行
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() 工作正常。 我做错了什么?如何解决这个问题?

【问题讨论】:

    标签: php symfony doctrine arraycollection


    【解决方案1】:

    您要查找的是 ArrayCollection 的 removeElement($element) 函数,而不是 remove($key) 函数。

    如其定义所示,remove($key) 函数从集合中删除指定索引 ($key) 处的元素,而 removeElement($element) 从集合中删除指定元素(如果找到)。

    由于您尝试将产品作为元素而不是按其索引删除,因此您应该使用removeElement($product)

    Doctrine ArrayCollection API 参考here

    【讨论】:

    • 非常感谢。这样可行。为什么add()removeElement()有这么大的区别?有addElement()removeElement()而不是这样的混乱不是更合乎逻辑......
    【解决方案2】:

    注意:您使用的是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);
    }
    

    【讨论】:

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