douzhongjiu2263 2018-09-18 22:38
浏览 63
已采纳

Symfony 3,ArrayCollection的remove()导致错误“警告:isset中的非法偏移类型或为空”

I have a Wishlist entity, which has relationship with a Product entity using MTM Doctrine annotation. I have the definition that $products is an Array Collection In Wishlist's __construct() and that's why I have addProduct() and removeProduct() methods. So, the class has the following view:

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

In my Controller, I have a place where I try to use removeProduct() method. I use it in the following way:

$wishlist->removeProduct($product);

But I receive the following error:

Warning: Illegal offset type in isset or empty (500 Internal Server Error)

It is at the line in

vendor\doctrine\collections\lib\Doctrine\Common\Collections\ArrayCollection.php at line 126

It has the following view:

public function remove($key)
{
    if ( ! isset($this->elements[$key]) && ! array_key_exists($key, $this->elements)) {
        return null;
    }
}

At the same time, addProduct() works fine. What do I do wrong? How can this issues be solved?

  • 写回答

2条回答 默认 最新

  • dongxun3424 2018-09-19 02:12
    关注

    What you are looking for is ArrayCollection's removeElement($element) function and not remove($key) function.

    As indicated by their definitions remove($key) function removes the element at the specified index ($key) from a collection, while removeElement($element) removes the specified element from the collection, if it is found.

    Since you are trying to remove a product as an element and not by its index you should use removeElement($product).

    Doctrine ArrayCollection API Reference here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退