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
|
5 |
|
public function __construct(RestRepository $repository, $mappedBy, RestRepository $related, $transformer) |
28
|
|
|
{ |
29
|
5 |
|
parent::__construct($repository, $transformer); |
30
|
5 |
|
$this->mappedBy = $mappedBy; |
31
|
5 |
|
$this->related = $related; |
32
|
5 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Related repository used as default repository for collection queries. |
36
|
|
|
* |
37
|
|
|
* @return RestRepository |
38
|
|
|
*/ |
39
|
5 |
|
public function repository() |
40
|
|
|
{ |
41
|
5 |
|
return $this->related; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Base entity repository. |
46
|
|
|
* |
47
|
|
|
* @return RestRepository |
48
|
|
|
*/ |
49
|
5 |
|
public function base() |
50
|
|
|
{ |
51
|
5 |
|
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
|
5 |
|
protected function applyFilter(RestRequestContract $request, QueryBuilder $qb) |
64
|
|
|
{ |
65
|
5 |
|
$entity = $this->base()->findById($request->getId()); |
66
|
|
|
|
67
|
5 |
|
$relateCriteria = Criteria::create(); |
68
|
5 |
|
$relateCriteria->andWhere($relateCriteria->expr()->eq($this->mappedBy(), $entity->getId())); |
69
|
|
|
|
70
|
5 |
|
$qb->innerJoin($qb->getRootAliases()[0].'.'.$this->mappedBy(), $this->mappedBy()); |
71
|
5 |
|
$qb->addCriteria($relateCriteria); |
72
|
|
|
|
73
|
5 |
|
parent::applyFilter($request, $qb); |
74
|
5 |
|
} |
75
|
|
|
} |
76
|
|
|
|