GET for relationship fixed: associated relation displayed

This commit is contained in:
Julie Lenaerts 2021-10-26 14:49:12 +02:00
parent 18ca01b0dc
commit b7466c7e85
3 changed files with 41 additions and 7 deletions

View File

@ -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']]);
}

View File

@ -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()

View File

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace Chill\PersonBundle\Serializer\Normalizer;
use Chill\PersonBundle\Entity\Relationships\Relationship;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectToPopulateTrait;
class RelationshipNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;
use ObjectToPopulateTrait;
use DenormalizerAwareTrait;
public function normalize($relationship, ?string $format = null, array $context = [])
{
return [
'type' => '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;
}
}