我有图像和类别实体。多对多联接表(One-dircetional)。我正在使用Doctirne的ObjectSelect表单元素和多个选项。当我选择一些类别时,Doctrine会成功插入记录。但是当我想要解除它们时 - 它却没有。我是否正确实施了removeCategories方法?可能是我错过了一些小事,但无法弄清楚
Class Image
...
/**
* @ORM\ManyToMany(targetEntity="Categories")
* @ORM\JoinTable(name="images_categories",
* joinColumns={@ORM\JoinColumn(name="image_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id")}
* )
*/
protected $classBobjects;
...
public function getCategories()
{
return $this->categories;
}
public function addCategories($categories)
{
foreach ($categories as $category) {
$this->categories->add($category);
}
}
public function removeCategories($categories)
{
foreach ($categories as $category) {
$this->categories->remove($category);
}
}
在表单字段集中,我有以下元素
$this->add([
'name' => 'categories',
'type' => ObjectSelect::class,
'attributes' => [
'multiple' => true,
'class' => 'chosen-select',
],
'options' => array(
'object_manager' => $this->getServiceLocator()->get(EntityManager::class),
'target_class' => Category::class,
'property' => 'title',
'is_method' => true,
'find_method' => array(
'name' => 'findAll',
),
),
]);
答案 0 :(得分:0)
我找到了。错误在于实现removeCategories()
$this->categories->remove($category);
should be
$this->categories->removeElement($category);