From b7466c7e85291b449e164abd9d48628f916f8f92 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Tue, 26 Oct 2021 14:49:12 +0200 Subject: [PATCH] GET for relationship fixed: associated relation displayed --- .../Controller/RelationshipApiController.php | 7 +--- .../Relationships/RelationshipRepository.php | 4 +- .../Normalizer/RelationshipNormalizer.php | 37 +++++++++++++++++++ 3 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 src/Bundle/ChillPersonBundle/Serializer/Normalizer/RelationshipNormalizer.php diff --git a/src/Bundle/ChillPersonBundle/Controller/RelationshipApiController.php b/src/Bundle/ChillPersonBundle/Controller/RelationshipApiController.php index 0327d335e..921f2d151 100644 --- a/src/Bundle/ChillPersonBundle/Controller/RelationshipApiController.php +++ b/src/Bundle/ChillPersonBundle/Controller/RelationshipApiController.php @@ -23,12 +23,6 @@ class RelationshipApiController extends ApiController $this->repository = $repository; } - public function createEntity(string $action, Request $request): object - { - $relationship = parent::createEntity($action, $request); - return $relationship; - } - /** * @Route("/api/1.0/relation/relationship/by-person/{person_id}.json", * name="chill_relation_relationship_by_person") @@ -39,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/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