Symfony 4.1:ArrayCollection 上的 UniqueEntity 验证
Symfony 4.1 : UniqueEntity validation on ArrayCollection
我有一个父实体Product,怎么可以在多个Family中。
系列 属性 显示为 CollectionType。目的是想创建多少family就创建多少,但是每个family的名称对于产品来说必须是唯一的。
当我有家庭数据时,它工作正常table;但是当它为空并且我一次为我的产品添加两个具有相同名称的系列时,不会触发@UniqueEntity;否则就是 ORM uniqueConstraints 如何响应。
An exception occurred while executing 'INSERT INTO
family (name, max_product, required, created_at,
updated_at, promotion_id) VALUES (?, ?, ?, ?, ?, ?)' with params
["toto", 1, 0, "2018-12-06 14:54:13", "2018-12-06 14:54:13", 1]:
下面是我的实体示例:
/**
* @var Family[]|ArrayCollection
*
* @ORM\OneToMany(targetEntity=Family::class, mappedBy="product", fetch="LAZY", cascade={"persist", "remove"}, orphanRemoval=true)
* @ORM\OrderBy({"id" = "ASC"})
* @Assert\Valid
*/
private $families;
和
/**
* @ORM\Entity(repositoryClass=ProductCollectionFamilyRepository::class)
* @ORM\Table(name="product_collection_family",
* indexes={@ORM\Index(name="IDX_FAMILY_NAME", columns={"name"})},
* uniqueConstraints={
* @ORM\UniqueConstraint(columns={"promotion_id", "name"})
* }
* )
*
* 2.5.4.5RG02
*
* @UniqueEntity(
* fields={"product", "name"},
* errorPath="name",
* message="backend.error.label.family.unique_entity"
* )
*/
class Family
{
use TimestampableEntity;
use ProductCollectionTrait;
/**
* @var int
*
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var string
*
* @ORM\Column(type="string", length=100)
* @Assert\NotBlank
* @Assert\Length(min="1", max="100", minMessage="backend.error.label.short", maxMessage="backend.error.label.long")
*/
private $name;
/**
* @var Product
*
* @ORM\ManyToOne(targetEntity=Product::class, inversedBy="families", fetch="LAZY")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=true)
* @Assert\NotBlank
*/
private $product;
}
有什么想法吗?
为了做到这一点,我只是创建了一个断言回调函数来检查数组集合中的元素,然后构建一个违规。
/**
* @Assert\Callback
*
* @param ExecutionContextInterface $context
* @param $payload
*/
public function validate(ExecutionContextInterface $context, $payload)
{
$productFamilies = clone $this->getProductFamilies();
$productFamiliesFiltered = $this->getProductFamilies()->filter(function ($currentProductFamily) use ($productFamilies) {
// Remove current family from original collection
$productFamilies->removeElement($currentProductFamily);
foreach ($productFamilies as $data) {
//we have same name ?
if ($currentProductFamily->getName() === $data->getName()) {
return true;
}
}
return false;
});
if (0 !== $productCollectionFiltered->count()) {
$context->buildViolation('backend.error.label.product_family.unique_entity')
->atPath('productFamilies')
->addViolation();
}
}
我有一个父实体Product,怎么可以在多个Family中。
系列 属性 显示为 CollectionType。目的是想创建多少family就创建多少,但是每个family的名称对于产品来说必须是唯一的。
当我有家庭数据时,它工作正常table;但是当它为空并且我一次为我的产品添加两个具有相同名称的系列时,不会触发@UniqueEntity;否则就是 ORM uniqueConstraints 如何响应。
An exception occurred while executing 'INSERT INTO family (name, max_product, required, created_at, updated_at, promotion_id) VALUES (?, ?, ?, ?, ?, ?)' with params ["toto", 1, 0, "2018-12-06 14:54:13", "2018-12-06 14:54:13", 1]:
下面是我的实体示例:
/**
* @var Family[]|ArrayCollection
*
* @ORM\OneToMany(targetEntity=Family::class, mappedBy="product", fetch="LAZY", cascade={"persist", "remove"}, orphanRemoval=true)
* @ORM\OrderBy({"id" = "ASC"})
* @Assert\Valid
*/
private $families;
和
/**
* @ORM\Entity(repositoryClass=ProductCollectionFamilyRepository::class)
* @ORM\Table(name="product_collection_family",
* indexes={@ORM\Index(name="IDX_FAMILY_NAME", columns={"name"})},
* uniqueConstraints={
* @ORM\UniqueConstraint(columns={"promotion_id", "name"})
* }
* )
*
* 2.5.4.5RG02
*
* @UniqueEntity(
* fields={"product", "name"},
* errorPath="name",
* message="backend.error.label.family.unique_entity"
* )
*/
class Family
{
use TimestampableEntity;
use ProductCollectionTrait;
/**
* @var int
*
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var string
*
* @ORM\Column(type="string", length=100)
* @Assert\NotBlank
* @Assert\Length(min="1", max="100", minMessage="backend.error.label.short", maxMessage="backend.error.label.long")
*/
private $name;
/**
* @var Product
*
* @ORM\ManyToOne(targetEntity=Product::class, inversedBy="families", fetch="LAZY")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=true)
* @Assert\NotBlank
*/
private $product;
}
有什么想法吗?
为了做到这一点,我只是创建了一个断言回调函数来检查数组集合中的元素,然后构建一个违规。
/**
* @Assert\Callback
*
* @param ExecutionContextInterface $context
* @param $payload
*/
public function validate(ExecutionContextInterface $context, $payload)
{
$productFamilies = clone $this->getProductFamilies();
$productFamiliesFiltered = $this->getProductFamilies()->filter(function ($currentProductFamily) use ($productFamilies) {
// Remove current family from original collection
$productFamilies->removeElement($currentProductFamily);
foreach ($productFamilies as $data) {
//we have same name ?
if ($currentProductFamily->getName() === $data->getName()) {
return true;
}
}
return false;
});
if (0 !== $productCollectionFiltered->count()) {
$context->buildViolation('backend.error.label.product_family.unique_entity')
->atPath('productFamilies')
->addViolation();
}
}