ZF2 / Doctrine / 无法从 ArrayCollection 中删除所有元素

ZF2 / Doctrine / Cannot remove all elements from an ArrayCollection

我有 2 个实体,ProductCategory,具有如下定义的双向 ManyToMany 关联映射:

产品

/**
* @ORM\ManyToMany(targetEntity="Category", inversedBy="products")
* @ORM\JoinTable(name="products_categories")
*/
protected $categories;

// with these accessors

    public function addCategories(Collection $categories)
    {
        foreach ($categories as $category)
        {
            $this->addCategory($category);
        }

        return $this;
    }

    public function addCategory(Category $category)
    {
        if (!$this->categories->contains($category))
        {
            $this->categories->add($category);
            $category->addProduct($this);
        }

        return $this;
    }

    public function removeCategories(Collection $categories)
    {
        foreach ($categories as $category)
        {
            $this->removeCategory($category);
        }

        return $this;
    }

    public function removeCategory(Category $category)
    {
        if ($this->categories->contains($category))
        {
            $this->categories->removeElement($category);
            $category->removeProduct($this);
        }

        return $this;
    }

    public function getCategories()
    {
        return $this->categories;
    }

    public function setCategories($categories)
    {
        $this->categories = new ArrayCollection();
        foreach ($categories as $category)
        {
            $this->addCategory($category);
        }

        return $this;
    }

类别实体:

/**
* @ORM\ManyToMany(targetEntity="Product", mappedBy="categories")
*/
protected $products;

// And accessors

    public function addProducts(Collection $products)
    {
        foreach ($products as $product)
        {
            $this->addProduct($product);
        }

        return $this;
    }

    public function addProduct(Product $product)
    {
        if (!$this->products->contains($product))
        {
            $this->products->add($product);
            $product->addCategory($this);
        }

        return $this;
    }

    public function removeProducts(Collection $products)
    {
        foreach ($products as $product)
        {
            $this->removeProduct($product);
        }

        return $this;
    }

    public function removeProduct(Product $product)
    {
        if ($this->products->contains($product))
        {
            $this->products->removeElement($product);
        }

        return $this;
    }

    public function getProducts()
    {
        return $this->products;
    }

   public function setProducts($products)
    {
        $this->products = new ArrayCollection();
        foreach ($products as $product)
        {
            $this->addProduct($product);
        }

        return $this;
    }

问题是:在 ZF2 表格 select 多个产品类别 select 中,我无法删除最后一个条目或删除所有条目 ...

除了最后一个,一个接一个都可以。

编辑 如果我删除所有元素,则不会调用 removeCategories()。

如有任何帮助,我们将不胜感激

编辑 2

我找到了一个非常好的解决方案来避开这个错误......

事实上,当多个 select 为空时,它会从 $_POST 变量中消失...因此,在我的产品控制器中,在 editAction() 中,我进行了以下测试:

if (!isset($_POST['main']['categories']))
{
    $product->getCategories()->clear();
}

我不认为这个 hack 是一个好的解决方案。

所以,如果有人有更好的解决方案,我正在等待答案。 TY

应该是ZF的bug,见:

https://github.com/zendframework/zf2/issues/4492

和修复的 PR:

https://github.com/zendframework/zf2/pull/7447

您在 Edit 2 部分中的解决方案与我在我的代码中使用的解决方法相同,直到发布修复程序。

如上面评论中所述,覆盖 Form->prepareBindData 是一种解决方案。

class MyForm extends Form
{
    ...

    protected function prepareBindData(array $values, array $match)
    {
        return $values;
    }
}