Complete refactoring: replace inheritance with trait composition

Refactored remaining entities to use trait composition instead of inheritance:
- MeasurementUnit - uses all structural traits directly
- PartCustomState - uses all structural traits directly
- Manufacturer - uses all structural traits + CompanyTrait
- Supplier - uses all structural traits + CompanyTrait
- AttachmentType - uses all structural traits directly

All entities now use explicit trait composition with:
- DBElementTrait, NamedElementTrait, TimestampTrait
- AttachmentsTrait, MasterAttachmentTrait
- StructuralElementTrait, ParametersTrait
- CompanyTrait (for Manufacturer and Supplier)

All entities implement required interfaces directly instead of inheriting them.

Co-authored-by: jbtronics <5410681+jbtronics@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2026-01-03 00:14:22 +00:00
parent 87d26e7eac
commit ad77dea4fe
5 changed files with 287 additions and 32 deletions

View file

@ -52,12 +52,14 @@ use Symfony\Component\Validator\Constraints as Assert;
/**
* Class AttachmentType.
* @see \App\Tests\Entity\Attachments\AttachmentTypeTest
* @extends AbstractStructuralDBElement<AttachmentTypeAttachment, AttachmentTypeParameter>
*/
#[ORM\Entity(repositoryClass: StructuralDBElementRepository::class)]
#[ORM\Table(name: '`attachment_types`')]
#[ORM\Index(columns: ['name'], name: 'attachment_types_idx_name')]
#[ORM\Index(columns: ['parent_id', 'name'], name: 'attachment_types_idx_parent_name')]
#[ORM\HasLifecycleCallbacks]
#[ORM\EntityListeners([TreeCacheInvalidationListener::class])]
#[UniqueEntity(fields: ['name', 'parent'], message: 'structural.entity.unique_name', ignoreNull: false)]
#[ApiResource(
operations: [
new Get(security: 'is_granted("read", object)'),
@ -84,8 +86,16 @@ use Symfony\Component\Validator\Constraints as Assert;
#[ApiFilter(LikeFilter::class, properties: ["name", "comment"])]
#[ApiFilter(DateFilter::class, strategy: DateFilterInterface::EXCLUDE_NULL)]
#[ApiFilter(OrderFilter::class, properties: ['name', 'id', 'addedDate', 'lastModified'])]
class AttachmentType extends AbstractStructuralDBElement
class AttachmentType implements DBElementInterface, NamedElementInterface, TimeStampableInterface, HasAttachmentsInterface, HasMasterAttachmentInterface, StructuralElementInterface, HasParametersInterface, \Stringable, \JsonSerializable
{
use DBElementTrait;
use NamedElementTrait;
use TimestampTrait;
use AttachmentsTrait;
use MasterAttachmentTrait;
use StructuralElementTrait;
use ParametersTrait;
#[ORM\OneToMany(mappedBy: 'parent', targetEntity: AttachmentType::class, cascade: ['persist'])]
#[ORM\OrderBy(['name' => Criteria::ASC])]
protected Collection $children;
@ -94,7 +104,10 @@ class AttachmentType extends AbstractStructuralDBElement
#[ORM\JoinColumn(name: 'parent_id')]
#[Groups(['attachment_type:read', 'attachment_type:write'])]
#[ApiProperty(readableLink: true, writableLink: false)]
protected ?AbstractStructuralDBElement $parent = null;
protected ?self $parent = null;
#[Groups(['attachment_type:read', 'attachment_type:write'])]
protected string $comment = '';
/**
* @var string A comma separated list of file types, which are allowed for attachment files.
@ -123,6 +136,7 @@ class AttachmentType extends AbstractStructuralDBElement
/** @var Collection<int, AttachmentTypeParameter>
*/
#[Assert\Valid]
#[UniqueObjectCollection(fields: ['name', 'group', 'element'])]
#[ORM\OneToMany(mappedBy: 'element', targetEntity: AttachmentTypeParameter::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
#[ORM\OrderBy(['group' => Criteria::ASC, 'name' => 'ASC'])]
#[Groups(['attachment_type:read', 'attachment_type:write', 'import', 'full'])]
@ -142,13 +156,37 @@ class AttachmentType extends AbstractStructuralDBElement
public function __construct()
{
$this->initializeAttachments();
$this->initializeStructuralElement();
$this->children = new ArrayCollection();
$this->parameters = new ArrayCollection();
parent::__construct();
$this->attachments = new ArrayCollection();
$this->attachments_with_type = new ArrayCollection();
}
public function __clone()
{
if ($this->id) {
$this->cloneDBElement();
$this->cloneAttachments();
// We create a new object, so give it a new creation date
$this->addedDate = null;
//Deep clone parameters
$parameters = $this->parameters;
$this->parameters = new ArrayCollection();
foreach ($parameters as $parameter) {
$this->addParameter(clone $parameter);
}
}
}
public function jsonSerialize(): array
{
return ['@id' => $this->getID()];
}
/**
* Get all attachments ("Attachment" objects) with this type.
*

View file

@ -39,26 +39,44 @@ use ApiPlatform\OpenApi\Model\Operation;
use ApiPlatform\Serializer\Filter\PropertyFilter;
use App\ApiPlatform\Filter\LikeFilter;
use App\Entity\Attachments\Attachment;
use App\Entity\Base\AttachmentsTrait;
use App\Entity\Base\CompanyTrait;
use App\Entity\Base\DBElementTrait;
use App\Entity\Base\MasterAttachmentTrait;
use App\Entity\Base\NamedElementTrait;
use App\Entity\Base\StructuralElementTrait;
use App\Entity\Base\TimestampTrait;
use App\Entity\Contracts\CompanyInterface;
use App\Entity\Contracts\DBElementInterface;
use App\Entity\Contracts\HasAttachmentsInterface;
use App\Entity\Contracts\HasMasterAttachmentInterface;
use App\Entity\Contracts\HasParametersInterface;
use App\Entity\Contracts\NamedElementInterface;
use App\Entity\Contracts\StructuralElementInterface;
use App\Entity\Contracts\TimeStampableInterface;
use App\Entity\Parameters\ParametersTrait;
use App\EntityListeners\TreeCacheInvalidationListener;
use App\Repository\Parts\ManufacturerRepository;
use App\Entity\Base\AbstractStructuralDBElement;
use App\Validator\Constraints\UniqueObjectCollection;
use Doctrine\Common\Collections\ArrayCollection;
use App\Entity\Attachments\ManufacturerAttachment;
use App\Entity\Base\AbstractCompany;
use App\Entity\Parameters\ManufacturerParameter;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* This entity represents a manufacturer of a part (The company that produces the part).
*
* @extends AbstractCompany<ManufacturerAttachment, ManufacturerParameter>
*/
#[ORM\Entity(repositoryClass: ManufacturerRepository::class)]
#[ORM\Table('`manufacturers`')]
#[ORM\Index(columns: ['name'], name: 'manufacturer_name')]
#[ORM\Index(columns: ['parent_id', 'name'], name: 'manufacturer_idx_parent_name')]
#[ORM\HasLifecycleCallbacks]
#[ORM\EntityListeners([TreeCacheInvalidationListener::class])]
#[UniqueEntity(fields: ['name', 'parent'], message: 'structural.entity.unique_name', ignoreNull: false)]
#[ApiResource(
operations: [
new Get(security: 'is_granted("read", object)'),
@ -87,13 +105,22 @@ use Symfony\Component\Validator\Constraints as Assert;
#[ApiFilter(LikeFilter::class, properties: ["name", "comment"])]
#[ApiFilter(DateFilter::class, strategy: DateFilterInterface::EXCLUDE_NULL)]
#[ApiFilter(OrderFilter::class, properties: ['name', 'id', 'addedDate', 'lastModified'])]
class Manufacturer extends AbstractCompany
class Manufacturer implements DBElementInterface, NamedElementInterface, TimeStampableInterface, HasAttachmentsInterface, HasMasterAttachmentInterface, StructuralElementInterface, HasParametersInterface, CompanyInterface, \Stringable, \JsonSerializable
{
use DBElementTrait;
use NamedElementTrait;
use TimestampTrait;
use AttachmentsTrait;
use MasterAttachmentTrait;
use StructuralElementTrait;
use ParametersTrait;
use CompanyTrait;
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')]
#[ORM\JoinColumn(name: 'parent_id')]
#[Groups(['manufacturer:read', 'manufacturer:write'])]
#[ApiProperty(readableLink: false, writableLink: false)]
protected ?AbstractStructuralDBElement $parent = null;
protected ?self $parent = null;
#[ORM\OneToMany(mappedBy: 'parent', targetEntity: self::class)]
#[ORM\OrderBy(['name' => Criteria::ASC])]
@ -118,16 +145,50 @@ class Manufacturer extends AbstractCompany
/** @var Collection<int, ManufacturerParameter>
*/
#[Assert\Valid]
#[UniqueObjectCollection(fields: ['name', 'group', 'element'])]
#[ORM\OneToMany(mappedBy: 'element', targetEntity: ManufacturerParameter::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
#[ORM\OrderBy(['group' => Criteria::ASC, 'name' => 'ASC'])]
#[Groups(['manufacturer:read', 'manufacturer:write'])]
#[ApiProperty(readableLink: false, writableLink: true)]
protected Collection $parameters;
#[Groups(['manufacturer:read', 'manufacturer:write'])]
protected string $comment = '';
#[Groups(['manufacturer:read'])]
protected ?\DateTimeImmutable $addedDate = null;
#[Groups(['manufacturer:read'])]
protected ?\DateTimeImmutable $lastModified = null;
public function __construct()
{
parent::__construct();
$this->initializeAttachments();
$this->initializeStructuralElement();
$this->children = new ArrayCollection();
$this->attachments = new ArrayCollection();
$this->parameters = new ArrayCollection();
}
public function __clone()
{
if ($this->id) {
$this->cloneDBElement();
$this->cloneAttachments();
// We create a new object, so give it a new creation date
$this->addedDate = null;
//Deep clone parameters
$parameters = $this->parameters;
$this->parameters = new ArrayCollection();
foreach ($parameters as $parameter) {
$this->addParameter(clone $parameter);
}
}
}
public function jsonSerialize(): array
{
return ['@id' => $this->getID()];
}
}

View file

@ -39,12 +39,26 @@ use ApiPlatform\OpenApi\Model\Operation;
use ApiPlatform\Serializer\Filter\PropertyFilter;
use App\ApiPlatform\Filter\LikeFilter;
use App\Entity\Attachments\Attachment;
use App\Entity\Base\AttachmentsTrait;
use App\Entity\Base\DBElementTrait;
use App\Entity\Base\MasterAttachmentTrait;
use App\Entity\Base\NamedElementTrait;
use App\Entity\Base\StructuralElementTrait;
use App\Entity\Base\TimestampTrait;
use App\Entity\Contracts\DBElementInterface;
use App\Entity\Contracts\HasAttachmentsInterface;
use App\Entity\Contracts\HasMasterAttachmentInterface;
use App\Entity\Contracts\HasParametersInterface;
use App\Entity\Contracts\NamedElementInterface;
use App\Entity\Contracts\StructuralElementInterface;
use App\Entity\Contracts\TimeStampableInterface;
use App\Entity\Parameters\ParametersTrait;
use App\EntityListeners\TreeCacheInvalidationListener;
use App\Repository\Parts\MeasurementUnitRepository;
use App\Validator\Constraints\UniqueObjectCollection;
use Doctrine\DBAL\Types\Types;
use App\Entity\Base\AbstractStructuralDBElement;
use Doctrine\Common\Collections\ArrayCollection;
use App\Entity\Attachments\MeasurementUnitAttachment;
use App\Entity\Base\AbstractPartsContainingDBElement;
use App\Entity\Parameters\MeasurementUnitParameter;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
@ -56,14 +70,15 @@ use Symfony\Component\Validator\Constraints\Length;
/**
* This unit represents the unit in which the amount of parts in stock are measured.
* This could be something like N, grams, meters, etc...
*
* @extends AbstractPartsContainingDBElement<MeasurementUnitAttachment,MeasurementUnitParameter>
*/
#[UniqueEntity('unit')]
#[ORM\Entity(repositoryClass: MeasurementUnitRepository::class)]
#[ORM\Table(name: '`measurement_units`')]
#[ORM\Index(columns: ['name'], name: 'unit_idx_name')]
#[ORM\Index(columns: ['parent_id', 'name'], name: 'unit_idx_parent_name')]
#[ORM\HasLifecycleCallbacks]
#[ORM\EntityListeners([TreeCacheInvalidationListener::class])]
#[UniqueEntity(fields: ['name', 'parent'], message: 'structural.entity.unique_name', ignoreNull: false)]
#[ApiResource(
operations: [
new Get(security: 'is_granted("read", object)'),
@ -92,8 +107,15 @@ use Symfony\Component\Validator\Constraints\Length;
#[ApiFilter(LikeFilter::class, properties: ["name", "comment", "unit"])]
#[ApiFilter(DateFilter::class, strategy: DateFilterInterface::EXCLUDE_NULL)]
#[ApiFilter(OrderFilter::class, properties: ['name', 'id', 'addedDate', 'lastModified'])]
class MeasurementUnit extends AbstractPartsContainingDBElement
class MeasurementUnit implements DBElementInterface, NamedElementInterface, TimeStampableInterface, HasAttachmentsInterface, HasMasterAttachmentInterface, StructuralElementInterface, HasParametersInterface, \Stringable, \JsonSerializable
{
use DBElementTrait;
use NamedElementTrait;
use TimestampTrait;
use AttachmentsTrait;
use MasterAttachmentTrait;
use StructuralElementTrait;
use ParametersTrait;
/**
* @var string The unit symbol that should be used for the Unit. This could be something like "", g (for grams)
* or m (for meters).
@ -131,7 +153,7 @@ class MeasurementUnit extends AbstractPartsContainingDBElement
#[ORM\JoinColumn(name: 'parent_id')]
#[Groups(['measurement_unit:read', 'measurement_unit:write'])]
#[ApiProperty(readableLink: false, writableLink: false)]
protected ?AbstractStructuralDBElement $parent = null;
protected ?self $parent = null;
/**
* @var Collection<int, MeasurementUnitAttachment>
@ -150,6 +172,7 @@ class MeasurementUnit extends AbstractPartsContainingDBElement
/** @var Collection<int, MeasurementUnitParameter>
*/
#[Assert\Valid]
#[UniqueObjectCollection(fields: ['name', 'group', 'element'])]
#[ORM\OneToMany(mappedBy: 'element', targetEntity: MeasurementUnitParameter::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
#[ORM\OrderBy(['group' => Criteria::ASC, 'name' => 'ASC'])]
#[Groups(['measurement_unit:read', 'measurement_unit:write'])]
@ -201,9 +224,33 @@ class MeasurementUnit extends AbstractPartsContainingDBElement
}
public function __construct()
{
parent::__construct();
$this->initializeAttachments();
$this->initializeStructuralElement();
$this->children = new ArrayCollection();
$this->attachments = new ArrayCollection();
$this->parameters = new ArrayCollection();
}
public function __clone()
{
if ($this->id) {
$this->cloneDBElement();
$this->cloneAttachments();
// We create a new object, so give it a new creation date
$this->addedDate = null;
//Deep clone parameters
$parameters = $this->parameters;
$this->parameters = new ArrayCollection();
foreach ($parameters as $parameter) {
$this->addParameter(clone $parameter);
}
}
}
public function jsonSerialize(): array
{
return ['@id' => $this->getID()];
}
}

View file

@ -37,26 +37,42 @@ use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Serializer\Filter\PropertyFilter;
use App\ApiPlatform\Filter\LikeFilter;
use App\Entity\Base\AbstractPartsContainingDBElement;
use App\Entity\Base\AbstractStructuralDBElement;
use App\Entity\Base\AttachmentsTrait;
use App\Entity\Base\DBElementTrait;
use App\Entity\Base\MasterAttachmentTrait;
use App\Entity\Base\NamedElementTrait;
use App\Entity\Base\StructuralElementTrait;
use App\Entity\Base\TimestampTrait;
use App\Entity\Contracts\DBElementInterface;
use App\Entity\Contracts\HasAttachmentsInterface;
use App\Entity\Contracts\HasMasterAttachmentInterface;
use App\Entity\Contracts\HasParametersInterface;
use App\Entity\Contracts\NamedElementInterface;
use App\Entity\Contracts\StructuralElementInterface;
use App\Entity\Contracts\TimeStampableInterface;
use App\Entity\Parameters\PartCustomStateParameter;
use App\Entity\Parameters\ParametersTrait;
use App\EntityListeners\TreeCacheInvalidationListener;
use App\Repository\Parts\PartCustomStateRepository;
use App\Validator\Constraints\UniqueObjectCollection;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* This entity represents a custom part state.
* If an organisation uses Part-DB and has its custom part states, this is useful.
*
* @extends AbstractPartsContainingDBElement<PartCustomStateAttachment,PartCustomStateParameter>
*/
#[ORM\Entity(repositoryClass: PartCustomStateRepository::class)]
#[ORM\Table('`part_custom_states`')]
#[ORM\Index(columns: ['name'], name: 'part_custom_state_name')]
#[ORM\HasLifecycleCallbacks]
#[ORM\EntityListeners([TreeCacheInvalidationListener::class])]
#[UniqueEntity(fields: ['name', 'parent'], message: 'structural.entity.unique_name', ignoreNull: false)]
#[ApiResource(
operations: [
new Get(security: 'is_granted("read", object)'),
@ -72,8 +88,16 @@ use Symfony\Component\Validator\Constraints as Assert;
#[ApiFilter(LikeFilter::class, properties: ["name"])]
#[ApiFilter(DateFilter::class, strategy: DateFilterInterface::EXCLUDE_NULL)]
#[ApiFilter(OrderFilter::class, properties: ['name', 'id', 'addedDate', 'lastModified'])]
class PartCustomState extends AbstractPartsContainingDBElement
class PartCustomState implements DBElementInterface, NamedElementInterface, TimeStampableInterface, HasAttachmentsInterface, HasMasterAttachmentInterface, StructuralElementInterface, HasParametersInterface, \Stringable, \JsonSerializable
{
use DBElementTrait;
use NamedElementTrait;
use TimestampTrait;
use AttachmentsTrait;
use MasterAttachmentTrait;
use StructuralElementTrait;
use ParametersTrait;
/**
* @var string The comment info for this element as markdown
*/
@ -88,7 +112,7 @@ class PartCustomState extends AbstractPartsContainingDBElement
#[ORM\JoinColumn(name: 'parent_id')]
#[Groups(['part_custom_state:read', 'part_custom_state:write'])]
#[ApiProperty(readableLink: false, writableLink: false)]
protected ?AbstractStructuralDBElement $parent = null;
protected ?self $parent = null;
/**
* @var Collection<int, PartCustomStateAttachment>
@ -107,6 +131,7 @@ class PartCustomState extends AbstractPartsContainingDBElement
/** @var Collection<int, PartCustomStateParameter>
*/
#[Assert\Valid]
#[UniqueObjectCollection(fields: ['name', 'group', 'element'])]
#[ORM\OneToMany(mappedBy: 'element', targetEntity: PartCustomStateParameter::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
#[ORM\OrderBy(['name' => 'ASC'])]
#[Groups(['part_custom_state:read', 'part_custom_state:write'])]
@ -119,9 +144,33 @@ class PartCustomState extends AbstractPartsContainingDBElement
public function __construct()
{
parent::__construct();
$this->initializeAttachments();
$this->initializeStructuralElement();
$this->children = new ArrayCollection();
$this->attachments = new ArrayCollection();
$this->parameters = new ArrayCollection();
}
public function __clone()
{
if ($this->id) {
$this->cloneDBElement();
$this->cloneAttachments();
// We create a new object, so give it a new creation date
$this->addedDate = null;
//Deep clone parameters
$parameters = $this->parameters;
$this->parameters = new ArrayCollection();
foreach ($parameters as $parameter) {
$this->addParameter(clone $parameter);
}
}
}
public function jsonSerialize(): array
{
return ['@id' => $this->getID()];
}
}

View file

@ -39,12 +39,28 @@ use ApiPlatform\OpenApi\Model\Operation;
use ApiPlatform\Serializer\Filter\PropertyFilter;
use App\ApiPlatform\Filter\LikeFilter;
use App\Entity\Attachments\Attachment;
use App\Entity\Base\AttachmentsTrait;
use App\Entity\Base\CompanyTrait;
use App\Entity\Base\DBElementTrait;
use App\Entity\Base\MasterAttachmentTrait;
use App\Entity\Base\NamedElementTrait;
use App\Entity\Base\StructuralElementTrait;
use App\Entity\Base\TimestampTrait;
use App\Entity\Contracts\CompanyInterface;
use App\Entity\Contracts\DBElementInterface;
use App\Entity\Contracts\HasAttachmentsInterface;
use App\Entity\Contracts\HasMasterAttachmentInterface;
use App\Entity\Contracts\HasParametersInterface;
use App\Entity\Contracts\NamedElementInterface;
use App\Entity\Contracts\StructuralElementInterface;
use App\Entity\Contracts\TimeStampableInterface;
use App\Entity\Parameters\ParametersTrait;
use App\EntityListeners\TreeCacheInvalidationListener;
use App\Repository\Parts\SupplierRepository;
use App\Entity\PriceInformations\Orderdetail;
use App\Validator\Constraints\UniqueObjectCollection;
use Doctrine\Common\Collections\ArrayCollection;
use App\Entity\Attachments\SupplierAttachment;
use App\Entity\Base\AbstractCompany;
use App\Entity\Base\AbstractStructuralDBElement;
use App\Entity\Parameters\SupplierParameter;
use App\Entity\PriceInformations\Currency;
use App\Validator\Constraints\BigDecimal\BigDecimalPositiveOrZero;
@ -52,18 +68,20 @@ use App\Validator\Constraints\Selectable;
use Brick\Math\BigDecimal;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* This entity represents a supplier of parts (the company that sells the parts).
*
* @extends AbstractCompany<SupplierAttachment, SupplierParameter>
*/
#[ORM\Entity(repositoryClass: SupplierRepository::class)]
#[ORM\Table('`suppliers`')]
#[ORM\Index(columns: ['name'], name: 'supplier_idx_name')]
#[ORM\Index(columns: ['parent_id', 'name'], name: 'supplier_idx_parent_name')]
#[ORM\HasLifecycleCallbacks]
#[ORM\EntityListeners([TreeCacheInvalidationListener::class])]
#[UniqueEntity(fields: ['name', 'parent'], message: 'structural.entity.unique_name', ignoreNull: false)]
#[ApiResource(
operations: [
new Get(security: 'is_granted("read", object)'),
@ -90,8 +108,17 @@ use Symfony\Component\Validator\Constraints as Assert;
#[ApiFilter(LikeFilter::class, properties: ["name", "comment"])]
#[ApiFilter(DateFilter::class, strategy: DateFilterInterface::EXCLUDE_NULL)]
#[ApiFilter(OrderFilter::class, properties: ['name', 'id', 'addedDate', 'lastModified'])]
class Supplier extends AbstractCompany
class Supplier implements DBElementInterface, NamedElementInterface, TimeStampableInterface, HasAttachmentsInterface, HasMasterAttachmentInterface, StructuralElementInterface, HasParametersInterface, CompanyInterface, \Stringable, \JsonSerializable
{
use DBElementTrait;
use NamedElementTrait;
use TimestampTrait;
use AttachmentsTrait;
use MasterAttachmentTrait;
use StructuralElementTrait;
use ParametersTrait;
use CompanyTrait;
#[ORM\OneToMany(mappedBy: 'parent', targetEntity: self::class)]
#[ORM\OrderBy(['name' => Criteria::ASC])]
protected Collection $children;
@ -100,7 +127,7 @@ class Supplier extends AbstractCompany
#[ORM\JoinColumn(name: 'parent_id')]
#[Groups(['supplier:read', 'supplier:write'])]
#[ApiProperty(readableLink: false, writableLink: false)]
protected ?AbstractStructuralDBElement $parent = null;
protected ?self $parent = null;
/**
* @var Collection<int, Orderdetail>
@ -144,12 +171,21 @@ class Supplier extends AbstractCompany
/** @var Collection<int, SupplierParameter>
*/
#[Assert\Valid]
#[UniqueObjectCollection(fields: ['name', 'group', 'element'])]
#[ORM\OneToMany(mappedBy: 'element', targetEntity: SupplierParameter::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
#[ORM\OrderBy(['group' => Criteria::ASC, 'name' => 'ASC'])]
#[Groups(['supplier:read', 'supplier:write'])]
#[ApiProperty(readableLink: false, writableLink: true)]
protected Collection $parameters;
#[Groups(['supplier:read', 'supplier:write'])]
protected string $comment = '';
#[Groups(['supplier:read'])]
protected ?\DateTimeImmutable $addedDate = null;
#[Groups(['supplier:read'])]
protected ?\DateTimeImmutable $lastModified = null;
/**
* Gets the currency that should be used by default, when creating a orderdetail with this supplier.
*/
@ -198,10 +234,34 @@ class Supplier extends AbstractCompany
}
public function __construct()
{
parent::__construct();
$this->initializeAttachments();
$this->initializeStructuralElement();
$this->children = new ArrayCollection();
$this->orderdetails = new ArrayCollection();
$this->attachments = new ArrayCollection();
$this->parameters = new ArrayCollection();
}
public function __clone()
{
if ($this->id) {
$this->cloneDBElement();
$this->cloneAttachments();
// We create a new object, so give it a new creation date
$this->addedDate = null;
//Deep clone parameters
$parameters = $this->parameters;
$this->parameters = new ArrayCollection();
foreach ($parameters as $parameter) {
$this->addParameter(clone $parameter);
}
}
}
public function jsonSerialize(): array
{
return ['@id' => $this->getID()];
}
}