mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-25 08:05:00 +00:00
docgen normalization for relation
This commit is contained in:
@@ -11,18 +11,24 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\PersonBundle\Repository\Relationships;
|
||||
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\PersonBundle\Entity\Relationships\Relation;
|
||||
use Chill\PersonBundle\Entity\Relationships\Relationship;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Doctrine\Persistence\ObjectRepository;
|
||||
|
||||
class RelationshipRepository implements ObjectRepository
|
||||
{
|
||||
private EntityRepository $repository;
|
||||
|
||||
private EntityManagerInterface $em;
|
||||
|
||||
public function __construct(EntityManagerInterface $em)
|
||||
{
|
||||
$this->repository = $em->getRepository(Relationship::class);
|
||||
$this->em = $em;
|
||||
}
|
||||
|
||||
public function find($id): ?Relationship
|
||||
@@ -40,19 +46,42 @@ class RelationshipRepository implements ObjectRepository
|
||||
return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
|
||||
}
|
||||
|
||||
public function findByPerson($personId): array
|
||||
/**
|
||||
* @param Person $person
|
||||
* @return array|Relationship[]
|
||||
*/
|
||||
public function findByPerson(Person $person): array
|
||||
{
|
||||
// return all relationships of which person is part? or only where person is the fromPerson?
|
||||
return $this->repository->createQueryBuilder('r')
|
||||
->select('r, t') // entity Relationship
|
||||
->join('r.relation', 't')
|
||||
->where('r.fromPerson = :val')
|
||||
->orWhere('r.toPerson = :val')
|
||||
->setParameter('val', $personId)
|
||||
return $this->buildQueryByPerson($person)
|
||||
->select('r')
|
||||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
|
||||
public function countByPerson(Person $person): int
|
||||
{
|
||||
return $this->buildQueryByPerson($person)
|
||||
->select('COUNT(p)')
|
||||
->getQuery()
|
||||
->getSingleScalarResult();
|
||||
}
|
||||
|
||||
private function buildQueryByPerson(Person $person): QueryBuilder
|
||||
{
|
||||
$qb = $this->em->createQueryBuilder();
|
||||
$qb
|
||||
->from(Relationship::class, 'r')
|
||||
->where(
|
||||
$qb->expr()->orX(
|
||||
$qb->expr()->eq('r.fromPerson', ':person'),
|
||||
$qb->expr()->eq('r.toPerson', ':person')
|
||||
)
|
||||
)
|
||||
->setParameter('person', $person);
|
||||
|
||||
return $qb;
|
||||
}
|
||||
|
||||
public function findOneBy(array $criteria): ?Relationship
|
||||
{
|
||||
return $this->findOneBy($criteria);
|
||||
|
Reference in New Issue
Block a user