diff --git a/src/Bundle/ChillPersonBundle/Controller/RelationshipApiController.php b/src/Bundle/ChillPersonBundle/Controller/RelationshipApiController.php index 5801b481e..921f2d151 100644 --- a/src/Bundle/ChillPersonBundle/Controller/RelationshipApiController.php +++ b/src/Bundle/ChillPersonBundle/Controller/RelationshipApiController.php @@ -10,6 +10,7 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Component\Routing\Annotation\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; +use Symfony\Component\HttpFoundation\Request; class RelationshipApiController extends ApiController { @@ -32,6 +33,7 @@ class RelationshipApiController extends ApiController { //TODO: add permissions? (voter?) $relationships = $this->repository->findByPerson($person); + dump($relationships[0]->getRelation()); return $this->json(\array_values($relationships), Response::HTTP_OK, [], ['groups' => [ 'read']]); } diff --git a/src/Bundle/ChillPersonBundle/Entity/Relationships/Relation.php b/src/Bundle/ChillPersonBundle/Entity/Relationships/Relation.php index 1f08f2a9f..7cd000dd5 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Relationships/Relation.php +++ b/src/Bundle/ChillPersonBundle/Entity/Relationships/Relation.php @@ -6,6 +6,7 @@ use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity() + * @ORM\Table(name="chill_person_relations") */ class Relation { diff --git a/src/Bundle/ChillPersonBundle/Entity/Relationships/Relationship.php b/src/Bundle/ChillPersonBundle/Entity/Relationships/Relationship.php index 32dc3eac1..e748f9797 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Relationships/Relationship.php +++ b/src/Bundle/ChillPersonBundle/Entity/Relationships/Relationship.php @@ -40,8 +40,8 @@ class Relationship private $toPerson; /** - * @ORM\ManyToOne(targetEntity=Relation::class, inversedBy="relationships") - * @ORM\JoinColumn(nullable=false) + * @ORM\ManyToOne(targetEntity=Relation::class) + * @ORM\JoinColumn(nullable=false, name="relation_id", referencedColumnName="id") * @Assert\NotBlank() * @Groups({"read", "write"}) */ diff --git a/src/Bundle/ChillPersonBundle/Repository/Relationships/RelationshipRepository.php b/src/Bundle/ChillPersonBundle/Repository/Relationships/RelationshipRepository.php index ad251b4ff..00546e424 100644 --- a/src/Bundle/ChillPersonBundle/Repository/Relationships/RelationshipRepository.php +++ b/src/Bundle/ChillPersonBundle/Repository/Relationships/RelationshipRepository.php @@ -27,7 +27,9 @@ class RelationshipRepository extends ServiceEntityRepository { // return all relationships of which person is part? or only where person is the fromPerson? return $this->createQueryBuilder('r') - ->andWhere('r.fromPerson = :val') + ->select('r, t') // entity Relationship + ->join('r.relation', 't') + ->where('r.fromPerson = :val') ->orWhere('r.toPerson = :val') ->setParameter('val', $personId) ->getQuery() diff --git a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/RelationshipNormalizer.php b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/RelationshipNormalizer.php new file mode 100644 index 000000000..6036a18e9 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/RelationshipNormalizer.php @@ -0,0 +1,37 @@ + 'relationship', + 'fromPerson' => $this->normalizer->normalize($relationship->getFromPerson()), + 'toPerson' => $this->normalizer->normalize($relationship->getToPerson()), + 'relation' => $this->normalizer->normalize($relationship->getRelation()), + 'reverse' => $relationship->getReverse() + ]; + } + + public function supportsNormalization($data, ?string $format = null) + { + return $data instanceof Relationship; + } + +} \ No newline at end of file diff --git a/src/Bundle/ChillPersonBundle/migrations/Version20211022132755.php b/src/Bundle/ChillPersonBundle/migrations/Version20211025141226.php similarity index 80% rename from src/Bundle/ChillPersonBundle/migrations/Version20211022132755.php rename to src/Bundle/ChillPersonBundle/migrations/Version20211025141226.php index f988e7ab5..9d67b0f4d 100644 --- a/src/Bundle/ChillPersonBundle/migrations/Version20211022132755.php +++ b/src/Bundle/ChillPersonBundle/migrations/Version20211025141226.php @@ -8,41 +8,42 @@ use Doctrine\DBAL\Schema\Schema; use Doctrine\Migrations\AbstractMigration; /** - * Entities Relation and Relationship created. + * Create the relationship and relation entity */ -final class Version20211022132755 extends AbstractMigration +final class Version20211025141226 extends AbstractMigration { public function getDescription(): string { - return ''; + return 'Create the relationship and relation entity'; } public function up(Schema $schema): void { - $this->addSql('CREATE SEQUENCE Relation_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); + $this->addSql('CREATE SEQUENCE chill_person_relations_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); $this->addSql('CREATE SEQUENCE chill_person_relationships_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); - $this->addSql('CREATE TABLE Relation (id INT NOT NULL, title JSON DEFAULT NULL, reverseTitle JSON DEFAULT NULL, PRIMARY KEY(id))'); + $this->addSql('CREATE TABLE chill_person_relations (id INT NOT NULL, title JSON DEFAULT NULL, reverseTitle JSON DEFAULT NULL, PRIMARY KEY(id))'); $this->addSql('CREATE TABLE chill_person_relationships (id INT NOT NULL, relation_id INT NOT NULL, reverse BOOLEAN NOT NULL, createdAt TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, updatedAt TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, fromPerson_id INT NOT NULL, toPerson_id INT NOT NULL, createdBy_id INT NOT NULL, updatedBy_id INT DEFAULT NULL, PRIMARY KEY(id))'); $this->addSql('CREATE INDEX IDX_23D47C51CBA59C1E ON chill_person_relationships (fromPerson_id)'); $this->addSql('CREATE INDEX IDX_23D47C514013E22A ON chill_person_relationships (toPerson_id)'); + $this->addSql('CREATE INDEX IDX_23D47C513256915B ON chill_person_relationships (relation_id)'); $this->addSql('CREATE INDEX IDX_23D47C513174800F ON chill_person_relationships (createdBy_id)'); $this->addSql('CREATE INDEX IDX_23D47C5165FF1AEC ON chill_person_relationships (updatedBy_id)'); - $this->addSql('CREATE INDEX IDX_23D47C513256915B ON chill_person_relationships (relation_id)'); $this->addSql('COMMENT ON COLUMN chill_person_relationships.createdAt IS \'(DC2Type:datetime_immutable)\''); $this->addSql('COMMENT ON COLUMN chill_person_relationships.updatedAt IS \'(DC2Type:datetime_immutable)\''); $this->addSql('ALTER TABLE chill_person_relationships ADD CONSTRAINT FK_23D47C51CBA59C1E FOREIGN KEY (fromPerson_id) REFERENCES chill_person_person (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); $this->addSql('ALTER TABLE chill_person_relationships ADD CONSTRAINT FK_23D47C514013E22A FOREIGN KEY (toPerson_id) REFERENCES chill_person_person (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('ALTER TABLE chill_person_relationships ADD CONSTRAINT FK_23D47C513256915B FOREIGN KEY (relation_id) REFERENCES chill_person_relations (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); $this->addSql('ALTER TABLE chill_person_relationships ADD CONSTRAINT FK_23D47C513174800F FOREIGN KEY (createdBy_id) REFERENCES users (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); $this->addSql('ALTER TABLE chill_person_relationships ADD CONSTRAINT FK_23D47C5165FF1AEC FOREIGN KEY (updatedBy_id) REFERENCES users (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); - $this->addSql('ALTER TABLE chill_person_relationships ADD CONSTRAINT FK_23D47C513256915B FOREIGN KEY (relation_id) REFERENCES Relation (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); } public function down(Schema $schema): void { + $this->addSql('ALTER TABLE chill_person_relationships DROP CONSTRAINT FK_23D47C513256915B'); - $this->addSql('DROP SEQUENCE Relation_id_seq CASCADE'); + $this->addSql('DROP SEQUENCE chill_person_relations_id_seq CASCADE'); $this->addSql('DROP SEQUENCE chill_person_relationships_id_seq CASCADE'); - $this->addSql('DROP TABLE Relation'); + $this->addSql('DROP TABLE chill_person_relations'); $this->addSql('DROP TABLE chill_person_relationships'); } }