2

我正在尝试通过多对多关系中的集合保存表单,问题是持久化对象!

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 }

我在哪里做错了?

4

1 回答 1

1

发生这种情况是因为您必须在持久化时手动检查子类别是否存在。您可以使用datatransformer或将检查代码放在addSubCategory实体的方法中,甚至可以使用prePresist生命周期回调。逻辑如下:您获取所有现有的子类别,然后遍历输入的子类别。如果在现有子类别中找到子类别,则将现有实体添加到结果 ArrayCollection 中,而不是传递输入的子类别。这是我在项目中使用的带有标签的相同任务的示例代码:

$existingSubcategories = $repository->findAll();

if (count($existingSubcategories) < 1)
    return $enteredSubcategories;

$resultCollection = new \Doctrine\Common\Collections\ArrayCollection();

foreach ($enteredSubcategories as $enteredSubcategory)
{
    $exists = false;
    foreach ($existingSubcategories as $existingSubcategory)
    {
        // compare them by any unique param
        if ($existingSubcategory->getName() == $enteredSubcategory->getName())
        {
            $exists = true;
            break;
        }
    }

    $resultCollection[] = ($exists === true) ? $existingSubcategory : $enteredSubcategory;
}

return $resultCollection;
于 2013-02-26T19:07:23.373 回答