1
|
|
|
<?php namespace Pz\Doctrine\Rest\Action; |
2
|
|
|
|
3
|
|
|
use Doctrine\Common\Collections\Criteria; |
4
|
|
|
use Doctrine\ORM\QueryBuilder; |
5
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator; |
6
|
|
|
use League\Fractal\Pagination\DoctrinePaginatorAdapter; |
7
|
|
|
use League\Fractal\Resource\Collection; |
8
|
|
|
use League\Fractal\TransformerAbstract; |
9
|
|
|
use Pz\Doctrine\Rest\Contracts\RestRequestContract; |
10
|
|
|
use Pz\Doctrine\Rest\RestResponse; |
11
|
|
|
use Pz\Doctrine\Rest\RestRepository; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Action for providing collection (list or array) of data with API. |
15
|
|
|
*/ |
16
|
|
|
class RelatedCollectionAction extends CollectionAction |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Field that contains the related resourse key |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $relatedField; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* RelatedCollectionAction constructor. |
27
|
|
|
* |
28
|
|
|
* @param string $relatedField Relation property on related entity |
29
|
|
|
* @param RestRepository $repository |
30
|
|
|
* @param TransformerAbstract $transformer |
31
|
|
|
*/ |
32
|
1 |
|
public function __construct($relatedField, RestRepository $repository, $transformer) |
33
|
|
|
{ |
34
|
1 |
|
parent::__construct($repository, $transformer); |
35
|
1 |
|
$this->relatedField = $relatedField; |
36
|
1 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Add filter by relation entity. |
40
|
|
|
* |
41
|
|
|
* @param RestRequestContract $request |
42
|
|
|
* @param QueryBuilder $qb |
43
|
|
|
* |
44
|
|
|
* @return $this |
45
|
|
|
* @throws \Pz\Doctrine\Rest\Exceptions\RestException |
46
|
|
|
*/ |
47
|
1 |
|
protected function applyFilter(RestRequestContract $request, QueryBuilder $qb) |
48
|
|
|
{ |
49
|
1 |
|
$entity = $this->repository()->findByIdentifier($request); |
50
|
|
|
|
51
|
1 |
|
$relateCriteria = Criteria::create(); |
52
|
1 |
|
$relateCriteria->andWhere($relateCriteria->expr()->eq($this->relatedField, $entity)); |
53
|
|
|
|
54
|
1 |
|
return parent::applyFilter($request, $qb->addCriteria($relateCriteria)); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|