Use new UniqueObjectCollection constraint to ensure that BOM entries does not contain duplicate items

This commit is contained in:
Jan Böhmer 2023-07-02 20:49:10 +02:00
parent 7b87b00b44
commit e72b120c12
7 changed files with 223 additions and 7 deletions

View file

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace App\Entity\ProjectSystem;
use App\Repository\Parts\DeviceRepository;
use App\Validator\Constraints\UniqueObjectCollection;
use Doctrine\DBAL\Types\Types;
use App\Entity\Attachments\ProjectAttachment;
use App\Entity\Base\AbstractStructuralDBElement;
@ -56,6 +57,8 @@ class Project extends AbstractStructuralDBElement
#[Assert\Valid]
#[Groups(['extended', 'full'])]
#[ORM\OneToMany(targetEntity: ProjectBOMEntry::class, mappedBy: 'project', cascade: ['persist', 'remove'], orphanRemoval: true)]
#[UniqueObjectCollection(fields: ['part'], message: 'project.bom_entry.part_already_in_bom')]
#[UniqueObjectCollection(fields: ['name'], message: 'project.bom_entry.name_already_in_bom')]
protected Collection $bom_entries;
#[ORM\Column(type: Types::INTEGER)]

View file

@ -22,6 +22,7 @@ declare(strict_types=1);
namespace App\Entity\ProjectSystem;
use App\Validator\UniqueValidatableInterface;
use Doctrine\DBAL\Types\Types;
use App\Entity\Base\AbstractDBElement;
use App\Entity\Base\TimestampTrait;
@ -38,12 +39,10 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface;
/**
* The ProjectBOMEntry class represents an entry in a project's BOM.
*/
#[UniqueEntity(fields: ['part', 'project'], message: 'project.bom_entry.part_already_in_bom')]
#[UniqueEntity(fields: ['name', 'project'], message: 'project.bom_entry.name_already_in_bom', ignoreNull: true)]
#[ORM\HasLifecycleCallbacks]
#[ORM\Entity]
#[ORM\Table('project_bom_entries')]
class ProjectBOMEntry extends AbstractDBElement
class ProjectBOMEntry extends AbstractDBElement implements UniqueValidatableInterface
{
use TimestampTrait;
@ -270,4 +269,11 @@ class ProjectBOMEntry extends AbstractDBElement
}
public function getComparableFields(): array
{
return [
'name' => $this->getName(),
'part' => $this->getPart()?->getID(),
];
}
}