diff --git a/migrations/Version20260406092549.php b/migrations/Version20260406092549.php new file mode 100644 index 0000000..b777dd0 --- /dev/null +++ b/migrations/Version20260406092549.php @@ -0,0 +1,39 @@ +addSql('ALTER TABLE software_profile DROP FOREIGN KEY FK_B70C3C9BFB84408A'); + $this->addSql('DROP INDEX IDX_B70C3C9BFB84408A ON software_profile'); + $this->addSql('ALTER TABLE software_profile ADD image_id INT DEFAULT NULL, DROP organizational_unit_id'); + $this->addSql('ALTER TABLE software_profile ADD CONSTRAINT FK_B70C3C9B3DA5256D FOREIGN KEY (image_id) REFERENCES image (id)'); + $this->addSql('CREATE INDEX IDX_B70C3C9B3DA5256D ON software_profile (image_id)'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE software_profile DROP FOREIGN KEY FK_B70C3C9B3DA5256D'); + $this->addSql('DROP INDEX IDX_B70C3C9B3DA5256D ON software_profile'); + $this->addSql('ALTER TABLE software_profile ADD organizational_unit_id INT NOT NULL, DROP image_id'); + $this->addSql('ALTER TABLE software_profile ADD CONSTRAINT FK_B70C3C9BFB84408A FOREIGN KEY (organizational_unit_id) REFERENCES organizational_unit (id)'); + $this->addSql('CREATE INDEX IDX_B70C3C9BFB84408A ON software_profile (organizational_unit_id)'); + } +} diff --git a/migrations/Version20260406092901.php b/migrations/Version20260406092901.php new file mode 100644 index 0000000..cd08306 --- /dev/null +++ b/migrations/Version20260406092901.php @@ -0,0 +1,33 @@ +addSql('ALTER TABLE command_task DROP FOREIGN KEY FK_F3D475A8FB84408A'); + $this->addSql('ALTER TABLE command_task ADD CONSTRAINT FK_F3D475A8FB84408A FOREIGN KEY (organizational_unit_id) REFERENCES organizational_unit (id) ON DELETE SET NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE command_task DROP FOREIGN KEY FK_F3D475A8FB84408A'); + $this->addSql('ALTER TABLE command_task ADD CONSTRAINT FK_F3D475A8FB84408A FOREIGN KEY (organizational_unit_id) REFERENCES organizational_unit (id)'); + } +} diff --git a/src/Dto/Input/SoftwareProfileInput.php b/src/Dto/Input/SoftwareProfileInput.php index 87ee072..79331fe 100644 --- a/src/Dto/Input/SoftwareProfileInput.php +++ b/src/Dto/Input/SoftwareProfileInput.php @@ -3,8 +3,8 @@ namespace App\Dto\Input; use ApiPlatform\Metadata\ApiProperty; +use App\Dto\Output\ImageOutput; use App\Dto\Output\OperativeSystemOutput; -use App\Dto\Output\OrganizationalUnitOutput; use App\Entity\SoftwareProfile; use Symfony\Component\Serializer\Annotation\Groups; @@ -19,8 +19,8 @@ final class SoftwareProfileInput public ?string $comments = null; #[Groups(['software-profile:write'])] - #[ApiProperty(description: 'The organizational unit of the software profile')] - public ?OrganizationalUnitOutput $organizationalUnit = null; + #[ApiProperty(description: 'The image of the software profile')] + public ?ImageOutput $image = null; #[Groups(['software-profile:write'])] #[ApiProperty(description: 'The operative system of the software profile')] @@ -35,8 +35,8 @@ final class SoftwareProfileInput $this->description = $softwareProfile->getDescription(); $this->comments = $softwareProfile->getComments(); - if($softwareProfile->getOrganizationalUnit()) { - $this->organizationalUnit = new OrganizationalUnitOutput($softwareProfile->getOrganizationalUnit()); + if ($softwareProfile->getImage()) { + $this->image = new ImageOutput($softwareProfile->getImage()); } if($softwareProfile->getOperativeSystem()) { @@ -52,9 +52,7 @@ final class SoftwareProfileInput $softwareProfile->setDescription($this->description); $softwareProfile->setComments($this->comments); - if ($this->organizationalUnit) { - $softwareProfile->setOrganizationalUnit($this->organizationalUnit->getEntity()); - } + $softwareProfile->setImage($this->image?->getEntity()); if ($this->operativeSystem) { $softwareProfile->setOperativeSystem($this->operativeSystem->getEntity()); diff --git a/src/Dto/Output/ImageOutput.php b/src/Dto/Output/ImageOutput.php index 9e857fc..4ee6c2c 100644 --- a/src/Dto/Output/ImageOutput.php +++ b/src/Dto/Output/ImageOutput.php @@ -91,7 +91,7 @@ final class ImageOutput extends AbstractOutput parent::__construct($image); $this->name = $image->getName(); - $this->softwareProfile = $image->getSoftwareProfile() ? new SoftwareProfileOutput($image->getSoftwareProfile()) : null; + $this->softwareProfile = $image->getSoftwareProfile() ? new SoftwareProfileOutput($image->getSoftwareProfile(), false) : null; $this->imageRepositories = $image->getImageImageRepositories()->map( fn(ImageImageRepository $image) => new ImageImageRepositoryOutput($image) diff --git a/src/Dto/Output/SoftwareProfileOutput.php b/src/Dto/Output/SoftwareProfileOutput.php index b3945bf..715b935 100644 --- a/src/Dto/Output/SoftwareProfileOutput.php +++ b/src/Dto/Output/SoftwareProfileOutput.php @@ -8,6 +8,7 @@ use App\Entity\Software; use App\Entity\SoftwareProfile; use Symfony\Component\Serializer\Annotation\Groups; + #[Get(shortName: 'SoftwareProfile')] final class SoftwareProfileOutput extends AbstractOutput { @@ -27,10 +28,10 @@ final class SoftwareProfileOutput extends AbstractOutput #[Groups(['software-profile:read'])] #[ApiProperty( - description: 'La unidad organizativa asociada', + description: 'La imagen asociada al perfil de software', example: null )] - public ?OrganizationalUnitOutput $organizationalUnit = null; + public ?ImageOutput $image = null; #[Groups(['software-profile:read'])] #[ApiProperty( @@ -60,15 +61,15 @@ final class SoftwareProfileOutput extends AbstractOutput )] public ?string $createdBy = null; - public function __construct(SoftwareProfile $softwareProfile) + public function __construct(SoftwareProfile $softwareProfile, bool $includeImage = true) { parent::__construct($softwareProfile); $this->description = $softwareProfile->getDescription(); $this->comments = $softwareProfile->getComments(); - if($softwareProfile->getOrganizationalUnit()) { - $this->organizationalUnit = new OrganizationalUnitOutput($softwareProfile->getOrganizationalUnit()); + if ($includeImage && $softwareProfile->getImage()) { + $this->image = new ImageOutput($softwareProfile->getImage()); } if ($softwareProfile->getOperativeSystem()) { diff --git a/src/Entity/CommandTask.php b/src/Entity/CommandTask.php index 8bb719c..4caf661 100644 --- a/src/Entity/CommandTask.php +++ b/src/Entity/CommandTask.php @@ -26,6 +26,7 @@ class CommandTask extends AbstractEntity private ?array $parameters = null; #[ORM\ManyToOne] + #[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')] private ?OrganizationalUnit $organizationalUnit = null; /** diff --git a/src/Entity/OrganizationalUnit.php b/src/Entity/OrganizationalUnit.php index 75b5849..3bfb1f5 100644 --- a/src/Entity/OrganizationalUnit.php +++ b/src/Entity/OrganizationalUnit.php @@ -78,12 +78,6 @@ class OrganizationalUnit extends AbstractEntity #[ORM\Column(nullable: true)] private ?int $capacity = null; - /** - * @var Collection - */ - #[ORM\OneToMany(mappedBy: 'organizationalUnit', targetEntity: SoftwareProfile::class)] - private Collection $softwareProfiles; - #[ORM\ManyToOne(inversedBy: 'organizationalUnits')] private ?Subnet $subnet = null; @@ -106,7 +100,7 @@ class OrganizationalUnit extends AbstractEntity $this->organizationalUnits = new ArrayCollection(); $this->users = new ArrayCollection(); $this->clients = new ArrayCollection(); - $this->softwareProfiles = new ArrayCollection(); + } public function getDescription(): ?string @@ -354,36 +348,6 @@ class OrganizationalUnit extends AbstractEntity } } - /** - * @return Collection - */ - public function getSoftwareProfiles(): Collection - { - return $this->softwareProfiles; - } - - public function addSoftwareProfile(SoftwareProfile $softwareProfile): static - { - if (!$this->softwareProfiles->contains($softwareProfile)) { - $this->softwareProfiles->add($softwareProfile); - $softwareProfile->setOrganizationalUnit($this); - } - - return $this; - } - - public function removeSoftwareProfile(SoftwareProfile $softwareProfile): static - { - if ($this->softwareProfiles->removeElement($softwareProfile)) { - // set the owning side to null (unless already changed) - if ($softwareProfile->getOrganizationalUnit() === $this) { - $softwareProfile->setOrganizationalUnit(null); - } - } - - return $this; - } - public function getSubnet(): ?Subnet { return $this->subnet; diff --git a/src/Entity/SoftwareProfile.php b/src/Entity/SoftwareProfile.php index 38eac0f..86971fb 100644 --- a/src/Entity/SoftwareProfile.php +++ b/src/Entity/SoftwareProfile.php @@ -16,9 +16,9 @@ class SoftwareProfile extends AbstractEntity #[ORM\Column(length: 255, nullable: true)] private ?string $comments = null; - #[ORM\ManyToOne(inversedBy: 'softwareProfiles')] - #[ORM\JoinColumn(nullable: false)] - private ?OrganizationalUnit $organizationalUnit = null; + #[ORM\ManyToOne] + #[ORM\JoinColumn(nullable: true)] + private ?Image $image = null; /** * @var Collection @@ -66,14 +66,14 @@ class SoftwareProfile extends AbstractEntity return $this; } - public function getOrganizationalUnit(): ?OrganizationalUnit + public function getImage(): ?Image { - return $this->organizationalUnit; + return $this->image; } - public function setOrganizationalUnit(?OrganizationalUnit $organizationalUnit): static + public function setImage(?Image $image): static { - $this->organizationalUnit = $organizationalUnit; + $this->image = $image; return $this; } diff --git a/src/Factory/SoftwareProfileFactory.php b/src/Factory/SoftwareProfileFactory.php index d3d370c..2f368a8 100644 --- a/src/Factory/SoftwareProfileFactory.php +++ b/src/Factory/SoftwareProfileFactory.php @@ -3,7 +3,7 @@ namespace App\Factory; use App\Entity\SoftwareProfile; -use App\Model\OrganizationalUnitTypes; +use App\Factory\ImageFactory; use App\Repository\SoftwareProfileRepository; use Zenstruck\Foundry\ModelFactory; use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory; @@ -37,7 +37,7 @@ final class SoftwareProfileFactory extends ModelFactory 'description' => self::faker()->text(255), 'updatedAt' => self::faker()->dateTime(), 'operativeSystem' => OperativeSystemFactory::createOne()->_save(), - 'organizationalUnit' => OrganizationalUnitFactory::createOne(['type' => OrganizationalUnitTypes::ORGANIZATIONAL_UNIT])->_save(), + 'image' => ImageFactory::createOne()->_save(), ]; }