Perform the duplicate check of parameter names already on initial creation

This fixes issue #568
This commit is contained in:
Jan Böhmer 2024-04-28 19:38:39 +02:00
parent dfe4568991
commit b70d74ae4b
4 changed files with 12 additions and 2 deletions

View file

@ -26,6 +26,7 @@ use App\Entity\Attachments\Attachment;
use App\Entity\Parameters\AbstractParameter; use App\Entity\Parameters\AbstractParameter;
use App\Repository\StructuralDBElementRepository; use App\Repository\StructuralDBElementRepository;
use App\EntityListeners\TreeCacheInvalidationListener; use App\EntityListeners\TreeCacheInvalidationListener;
use App\Validator\Constraints\UniqueObjectCollection;
use Doctrine\DBAL\Types\Types; use Doctrine\DBAL\Types\Types;
use App\Entity\Attachments\AttachmentContainingDBElement; use App\Entity\Attachments\AttachmentContainingDBElement;
use App\Entity\Parameters\ParametersTrait; use App\Entity\Parameters\ParametersTrait;
@ -115,6 +116,7 @@ abstract class AbstractStructuralDBElement extends AttachmentContainingDBElement
* @phpstan-var Collection<int, PT> * @phpstan-var Collection<int, PT>
*/ */
#[Assert\Valid] #[Assert\Valid]
#[UniqueObjectCollection(fields: ['name', 'group', 'element'])]
protected Collection $parameters; protected Collection $parameters;
/** @var string[] all names of all parent elements as an array of strings, /** @var string[] all names of all parent elements as an array of strings,

View file

@ -53,6 +53,7 @@ use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post; use ApiPlatform\Metadata\Post;
use App\ApiPlatform\Filter\LikeFilter; use App\ApiPlatform\Filter\LikeFilter;
use App\Repository\ParameterRepository; use App\Repository\ParameterRepository;
use App\Validator\UniqueValidatableInterface;
use Doctrine\DBAL\Types\Types; use Doctrine\DBAL\Types\Types;
use App\Entity\Base\AbstractDBElement; use App\Entity\Base\AbstractDBElement;
use App\Entity\Base\AbstractNamedDBElement; use App\Entity\Base\AbstractNamedDBElement;
@ -94,7 +95,7 @@ use function sprintf;
#[ApiFilter(OrderFilter::class, properties: ['name', 'id', 'addedDate', 'lastModified'])] #[ApiFilter(OrderFilter::class, properties: ['name', 'id', 'addedDate', 'lastModified'])]
//This discriminator map is required for API platform to know which class to use for deserialization, when creating a new parameter. //This discriminator map is required for API platform to know which class to use for deserialization, when creating a new parameter.
#[DiscriminatorMap(typeProperty: '_type', mapping: self::API_DISCRIMINATOR_MAP)] #[DiscriminatorMap(typeProperty: '_type', mapping: self::API_DISCRIMINATOR_MAP)]
abstract class AbstractParameter extends AbstractNamedDBElement abstract class AbstractParameter extends AbstractNamedDBElement implements UniqueValidatableInterface
{ {
/* /*
@ -458,4 +459,9 @@ abstract class AbstractParameter extends AbstractNamedDBElement
{ {
return static::ALLOWED_ELEMENT_CLASS; return static::ALLOWED_ELEMENT_CLASS;
} }
public function getComparableFields(): array
{
return ['name' => $this->name, 'group' => $this->group, 'element' => $this->element?->getId()];
}
} }

View file

@ -54,6 +54,7 @@ use App\Entity\Parts\PartTraits\OrderTrait;
use App\Entity\Parts\PartTraits\ProjectTrait; use App\Entity\Parts\PartTraits\ProjectTrait;
use App\EntityListeners\TreeCacheInvalidationListener; use App\EntityListeners\TreeCacheInvalidationListener;
use App\Repository\PartRepository; use App\Repository\PartRepository;
use App\Validator\Constraints\UniqueObjectCollection;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
@ -119,6 +120,7 @@ class Part extends AttachmentContainingDBElement
#[Groups(['full', 'part:read', 'part:write'])] #[Groups(['full', 'part:read', 'part:write'])]
#[ORM\OneToMany(mappedBy: 'element', targetEntity: PartParameter::class, cascade: ['persist', 'remove'], orphanRemoval: true)] #[ORM\OneToMany(mappedBy: 'element', targetEntity: PartParameter::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
#[ORM\OrderBy(['group' => 'ASC', 'name' => 'ASC'])] #[ORM\OrderBy(['group' => 'ASC', 'name' => 'ASC'])]
#[UniqueObjectCollection(fields: ['name', 'group', 'element'])]
protected Collection $parameters; protected Collection $parameters;

View file

@ -34,7 +34,7 @@ class UniqueObjectCollection extends Constraint
self::IS_NOT_UNIQUE => 'IS_NOT_UNIQUE', self::IS_NOT_UNIQUE => 'IS_NOT_UNIQUE',
]; ];
public string $message = 'This collection should contain only unique elements.'; public string $message = 'This value is already used.';
public $normalizer; public $normalizer;
/** /**