-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create ActivityArea entity and relate it to Space.
- Loading branch information
1 parent
a38f48e
commit 591ec4e
Showing
6 changed files
with
443 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DoctrineMigrations; | ||
|
||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\Migrations\AbstractMigration; | ||
|
||
final class Version20250131015424 extends AbstractMigration | ||
{ | ||
public function getDescription(): string | ||
{ | ||
return 'Create ActivityArea entity and its relation with Space'; | ||
} | ||
|
||
public function up(Schema $schema): void | ||
{ | ||
$this->addSql('CREATE TABLE activity_area (id UUID NOT NULL, name VARCHAR(20) NOT NULL, PRIMARY KEY(id))'); | ||
$this->addSql('CREATE TABLE activity_area_spaces (activity_area_id UUID NOT NULL, space_id UUID NOT NULL, PRIMARY KEY(activity_area_id, space_id))'); | ||
|
||
$this->addSql('CREATE INDEX IDX_F15AB7E8BD5D367C ON activity_area_spaces (activity_area_id)'); | ||
$this->addSql('CREATE INDEX IDX_F15AB7E823575340 ON activity_area_spaces (space_id)'); | ||
|
||
$this->addSql('ALTER TABLE activity_area_spaces ADD CONSTRAINT FK_F15AB7E8BD5D367C FOREIGN KEY (activity_area_id) REFERENCES activity_area (id) ON DELETE CASCADE'); | ||
$this->addSql('ALTER TABLE activity_area_spaces ADD CONSTRAINT FK_F15AB7E823575340 FOREIGN KEY (space_id) REFERENCES space (id) ON DELETE CASCADE'); | ||
} | ||
|
||
public function down(Schema $schema): void | ||
{ | ||
$this->addSql('DROP TABLE activity_area_spaces'); | ||
$this->addSql('DROP TABLE activity_area'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\DataFixtures\Entity; | ||
|
||
use App\Entity\ActivityArea; | ||
use Doctrine\Common\DataFixtures\DependentFixtureInterface; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\Persistence\ObjectManager; | ||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
use Symfony\Component\Serializer\SerializerInterface; | ||
|
||
final class ActivityAreaFixtures extends AbstractFixture implements DependentFixtureInterface | ||
{ | ||
public const string ACTIVITY_AREA_ID_PREFIX = 'activity_area'; | ||
public const string ACTIVITY_AREA_ID_1 = 'a1b2c3d4-e5f6-7890-1234-56789abcdef0'; | ||
public const string ACTIVITY_AREA_ID_2 = 'b2c3d4e5-f678-9012-3456-789abcdef012'; | ||
public const string ACTIVITY_AREA_ID_3 = 'c3d4e5f6-7890-1234-5678-9abcdef01234'; | ||
public const string ACTIVITY_AREA_ID_4 = 'd4e5f678-9012-3456-789a-bcdef0123456'; | ||
public const string ACTIVITY_AREA_ID_5 = 'e5f67890-1234-5678-9abc-def012345678'; | ||
public const string ACTIVITY_AREA_ID_6 = 'f6789012-3456-789a-bcde-f01234567890'; | ||
public const string ACTIVITY_AREA_ID_7 = '67890123-4567-89ab-cdef-012345678901'; | ||
public const string ACTIVITY_AREA_ID_8 = '78901234-5678-9abc-def0-123456789012'; | ||
public const string ACTIVITY_AREA_ID_9 = '89012345-6789-abcd-ef01-234567890123'; | ||
public const string ACTIVITY_AREA_ID_10 = '90123456-789a-bcde-f012-345678901234'; | ||
|
||
public const array ACTIVITY_AREAS = [ | ||
[ | ||
'id' => self::ACTIVITY_AREA_ID_1, | ||
'name' => 'Artes Visuais', | ||
'spaces' => [ | ||
SpaceFixtures::SPACE_ID_1, | ||
SpaceFixtures::SPACE_ID_2, | ||
SpaceFixtures::SPACE_ID_3, | ||
], | ||
], | ||
[ | ||
'id' => self::ACTIVITY_AREA_ID_2, | ||
'name' => 'Música', | ||
'spaces' => [ | ||
SpaceFixtures::SPACE_ID_4, | ||
SpaceFixtures::SPACE_ID_5, | ||
SpaceFixtures::SPACE_ID_6, | ||
], | ||
], | ||
[ | ||
'id' => self::ACTIVITY_AREA_ID_3, | ||
'name' => 'Teatro', | ||
'spaces' => [ | ||
SpaceFixtures::SPACE_ID_7, | ||
SpaceFixtures::SPACE_ID_8, | ||
SpaceFixtures::SPACE_ID_9, | ||
], | ||
], | ||
[ | ||
'id' => self::ACTIVITY_AREA_ID_4, | ||
'name' => 'Dança', | ||
'spaces' => [ | ||
SpaceFixtures::SPACE_ID_10, | ||
SpaceFixtures::SPACE_ID_1, | ||
SpaceFixtures::SPACE_ID_2, | ||
], | ||
], | ||
[ | ||
'id' => self::ACTIVITY_AREA_ID_5, | ||
'name' => 'Cinema', | ||
'spaces' => [ | ||
SpaceFixtures::SPACE_ID_3, | ||
SpaceFixtures::SPACE_ID_4, | ||
SpaceFixtures::SPACE_ID_5, | ||
], | ||
], | ||
[ | ||
'id' => self::ACTIVITY_AREA_ID_6, | ||
'name' => 'Literatura', | ||
'spaces' => [ | ||
SpaceFixtures::SPACE_ID_6, | ||
SpaceFixtures::SPACE_ID_7, | ||
SpaceFixtures::SPACE_ID_8, | ||
], | ||
], | ||
[ | ||
'id' => self::ACTIVITY_AREA_ID_7, | ||
'name' => 'Gastronomia', | ||
'spaces' => [ | ||
SpaceFixtures::SPACE_ID_9, | ||
SpaceFixtures::SPACE_ID_10, | ||
SpaceFixtures::SPACE_ID_1, | ||
], | ||
], | ||
[ | ||
'id' => self::ACTIVITY_AREA_ID_8, | ||
'name' => 'Design', | ||
'spaces' => [ | ||
SpaceFixtures::SPACE_ID_2, | ||
SpaceFixtures::SPACE_ID_3, | ||
SpaceFixtures::SPACE_ID_4, | ||
], | ||
], | ||
[ | ||
'id' => self::ACTIVITY_AREA_ID_9, | ||
'name' => 'Artesanato', | ||
'spaces' => [ | ||
SpaceFixtures::SPACE_ID_5, | ||
SpaceFixtures::SPACE_ID_6, | ||
SpaceFixtures::SPACE_ID_7, | ||
], | ||
], | ||
[ | ||
'id' => self::ACTIVITY_AREA_ID_10, | ||
'name' => 'Fotografia', | ||
'spaces' => [ | ||
SpaceFixtures::SPACE_ID_8, | ||
SpaceFixtures::SPACE_ID_9, | ||
SpaceFixtures::SPACE_ID_10, | ||
], | ||
], | ||
]; | ||
|
||
public function __construct( | ||
protected EntityManagerInterface $entityManager, | ||
protected TokenStorageInterface $tokenStorage, | ||
private readonly SerializerInterface $serializer, | ||
) { | ||
parent::__construct($entityManager, $tokenStorage); | ||
} | ||
|
||
public function getDependencies() | ||
{ | ||
return [ | ||
SpaceFixtures::class, | ||
]; | ||
} | ||
|
||
public function load(ObjectManager $manager): void | ||
{ | ||
$this->truncateTable(ActivityArea::class); | ||
$this->createActivityAreas($manager); | ||
} | ||
|
||
private function createActivityAreas(ObjectManager $manager): void | ||
{ | ||
foreach (self::ACTIVITY_AREAS as $activityAreaData) { | ||
$activityArea = $this->serializer->denormalize($activityAreaData, ActivityArea::class); | ||
$this->setReference(self::ACTIVITY_AREA_ID_PREFIX.$activityAreaData['id'], $activityArea); | ||
$manager->persist($activityArea); | ||
} | ||
|
||
$manager->flush(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Entity; | ||
|
||
use App\Repository\ActivityAreaRepository; | ||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Symfony\Bridge\Doctrine\Types\UuidType; | ||
use Symfony\Component\Serializer\Attribute\Groups; | ||
use Symfony\Component\Uid\Uuid; | ||
|
||
#[ORM\Entity(repositoryClass: ActivityAreaRepository::class)] | ||
class ActivityArea | ||
{ | ||
#[ORM\Id] | ||
#[ORM\Column(type: UuidType::NAME)] | ||
#[Groups(['activity_area.get'])] | ||
private ?Uuid $id = null; | ||
|
||
#[ORM\Column(length: 20)] | ||
private ?string $name = null; | ||
|
||
#[ORM\JoinTable(name: 'activity_area_spaces')] | ||
#[ORM\JoinColumn(name: 'activity_area_id', referencedColumnName: 'id')] | ||
#[ORM\InverseJoinColumn(name: 'space_id', referencedColumnName: 'id')] | ||
#[ORM\ManyToMany(targetEntity: Space::class, inversedBy: 'ActivityArea', cascade: ['persist'])] | ||
private Collection $spaces; | ||
|
||
public function __construct() | ||
{ | ||
$this->spaces = new ArrayCollection(); | ||
} | ||
|
||
public function getId(): ?Uuid | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function setId(Uuid $id): void | ||
{ | ||
$this->id = $id; | ||
} | ||
|
||
public function getName(): ?string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function setName(string $name): void | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
public function getSpaces(): Collection | ||
{ | ||
return $this->spaces; | ||
} | ||
|
||
public function setSpaces(Collection $spaces): void | ||
{ | ||
$this->spaces = $spaces; | ||
} | ||
|
||
public function addSpace(Space $space): void | ||
{ | ||
if (!$this->spaces->contains($space)) { | ||
$this->spaces->add($space); | ||
} | ||
} | ||
|
||
public function removeSpace(Space $space): void | ||
{ | ||
$this->spaces->removeElement($space); | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return [ | ||
'id' => $this->id?->toRfc4122(), | ||
'name' => $this->name, | ||
'spaces' => $this->spaces->map(fn (Space $space) => $space->getId())->toArray(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.