0

对于一个集合,我想检查数据库中是否已经存在一个新添加的部分。如果是这样,它将被新值覆盖。

/**
 * Add Part
 */
public function addPart(\MyBundle\Entity\FooTypePart $Part)
{
    $part->setProduct($this);
    $this->part[] = $part;

    return $this;
}

/**
*/
public function removePart(\MyBundle\Entity\FooTypePart $part)
{
    $this->part->removeElement($part);
}

/**
 * Get Part
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getPart()
{
    return $this->part;
}

/**
 * Set Part
 */
public function setPart($part)
{
    $this->part = $part;
    return $this;
}

Part Entity 有:ID、Category_id (FK)、Product_id (FK)、Part (Collection)

目前可以添加具有相同名称的新部件,即使已经存在具有相同 Product_id 和 Category_id 的部件。使零件独一无二不是解决办法,因为零件可用于许多产品/类别。

以下示例已存在于数据库中,但“部分”不同。所以它应该做一个更新命令。

<?php
$part = new FooTypePart();
$part->setCategory($specification);
$part->setProduct($product);
$part->setPart('DifferentNamingThenCurrentOne');
$xx->addSpecificationValue($part);

如何?:-)

4

1 回答 1

0

Just use the UniqueEntity validator to find an already existant Item in the collection.

You can specify uniqueness using multiple properties or a repository method. This way you can search for items only having a unique combination of name, product-id and category-id.

Create a validation group i.e. "unique" and find the non-unique/existant entity in your collection by looking for invalid entities.

... then update the existing entity with your new value. You will probably need some extra logic because there might be multiple fields with the same name added to your form collection.

于 2013-07-10T03:00:48.633 回答