R3VoLuT1OneR /
doctrine-rest
| 1 | <?php namespace Pz\Doctrine\Rest\Action\Related; |
||
| 2 | |||
| 3 | use Pz\Doctrine\Rest\Action\CollectionAction as BaseCollectionAction; |
||
| 4 | use Pz\Doctrine\Rest\Contracts\RestRequestContract; |
||
| 5 | use Pz\Doctrine\Rest\RestRepository; |
||
| 6 | |||
| 7 | use Doctrine\Common\Collections\Criteria; |
||
| 8 | use Doctrine\ORM\QueryBuilder; |
||
| 9 | use League\Fractal\TransformerAbstract; |
||
| 10 | use Pz\Doctrine\Rest\Traits\RelatedAction; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Action for providing collection (list or array) of data with API. |
||
| 14 | */ |
||
| 15 | class RelatedCollectionAction extends BaseCollectionAction |
||
| 16 | { |
||
| 17 | use RelatedAction; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * RelatedRestAction constructor. |
||
| 21 | * |
||
| 22 | * @param RestRepository $repository |
||
| 23 | * @param string $mappedBy |
||
| 24 | * @param RestRepository $related |
||
| 25 | * @param TransformerAbstract $transformer |
||
| 26 | */ |
||
| 27 | 6 | public function __construct(RestRepository $repository, $mappedBy, RestRepository $related, $transformer) |
|
| 28 | { |
||
| 29 | 6 | parent::__construct($repository, $transformer); |
|
| 30 | 6 | $this->mappedBy = $mappedBy; |
|
| 31 | 6 | $this->related = $related; |
|
| 32 | 6 | } |
|
| 33 | |||
| 34 | /** |
||
| 35 | * Related repository used as default repository for collection queries. |
||
| 36 | * |
||
| 37 | * @return RestRepository |
||
| 38 | */ |
||
| 39 | 6 | public function repository() |
|
| 40 | { |
||
| 41 | 6 | return $this->related; |
|
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Base entity repository. |
||
| 46 | * |
||
| 47 | * @return RestRepository |
||
| 48 | */ |
||
| 49 | 6 | public function base() |
|
| 50 | { |
||
| 51 | 6 | return $this->repository; |
|
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Add filter by relation entity. |
||
| 56 | * |
||
| 57 | * @param RestRequestContract $request |
||
| 58 | * @param QueryBuilder $qb |
||
| 59 | * |
||
| 60 | * @return $this |
||
| 61 | * @throws \Pz\Doctrine\Rest\Exceptions\RestException |
||
| 62 | */ |
||
| 63 | 6 | protected function applyFilter(RestRequestContract $request, QueryBuilder $qb): self |
|
| 64 | { |
||
| 65 | 6 | $entity = $this->base()->findById($request->getId()); |
|
| 66 | |||
| 67 | 6 | $relateCriteria = Criteria::create(); |
|
| 68 | 6 | $relateCriteria->andWhere($relateCriteria->expr()->eq($this->mappedBy(), $entity->getId())); |
|
| 69 | |||
| 70 | 6 | $qb->innerJoin($qb->getRootAliases()[0].'.'.$this->mappedBy(), $this->mappedBy()); |
|
| 71 | 6 | $qb->addCriteria($relateCriteria); |
|
| 72 | |||
| 73 | 6 | return parent::applyFilter($request, $qb); |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 74 | 6 | } |
|
| 75 | } |
||
| 76 |