我正在尝试通过多对多关系中的集合保存表单,问题是持久化对象!
class Anagrafica
{
/**
* @ORM\ManyToMany(targetEntity="SubCategories", inversedBy="anagrafiche", cascade={"persist", "remove"})
* @ORM\JoinTable(name="AnCat")
**/
private $subCategories;
//..
public function __construct()
{
$this->subCategories = new \Doctrine\Common\Collections\ArrayCollection();
//..
}
/**
* Add subCategories
*
* @param \My\BusinessBundle\Entity\SubCategories $subCategories
* @return Anagrafica
*/
public function addSubCategory(\My\BusinessBundle\Entity\SubCategories $subCategories)
{
$subCategories->addAnagrafiche($this);
$this->subCategories[] = $subCategories;
}
*******
class SubCategories
{
/**
* @ORM\ManyToMany(targetEntity="Anagrafica", mappedBy="subCategories")
*/
private $anagrafiche;
public function __construct()
{
$this->anagrafiche = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add anagrafiche
*
* @param \My\BusinessBundle\Entity\Anagrafica $anagrafiche
* @return SubCategories
*/
public function addAnagrafiche(\My\BusinessBundle\Entity\Anagrafica $anagrafiche)
{
$this->anagrafiche[] = $anagrafiche;
}
Anagrafica 类型:
//..
->add('subCategories', 'collection', array('type' => new SubCategoriesType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'prototype_name' => '__categ__',
'by_reference' => false
))
当我去保存时,即使我选择了数据库中已经存在的子类别,他也会执行查询插入:
INSERT INTO SubCategories (subCategory, category_id) VALUES (?, ?)
Parameters: { 1: Object(Doctrine\Common\Collections\ArrayCollection), 2: 12 }
我在哪里做错了?