|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
/** |
|
4
|
|
|
* /src/Repository/Traits/RepositoryWrappersTrait.php |
|
5
|
|
|
* |
|
6
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace App\Repository\Traits; |
|
9
|
|
|
|
|
10
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
|
11
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
12
|
|
|
use Doctrine\Common\Proxy\Proxy; |
|
13
|
|
|
use Doctrine\ORM\EntityManager; |
|
14
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class RepositoryWrappersTrait |
|
18
|
|
|
* |
|
19
|
|
|
* @package App\Repository\Traits |
|
20
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
21
|
|
|
* |
|
22
|
|
|
* @method string getEntityName(): string |
|
23
|
|
|
*/ |
|
24
|
|
|
trait RepositoryWrappersTrait |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var ManagerRegistry |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $managerRegistry; |
|
30
|
|
|
|
|
31
|
|
|
/** @noinspection GenericObjectTypeUsageInspection */ |
|
32
|
|
|
/** |
|
33
|
|
|
* Gets a reference to the entity identified by the given type and identifier without actually loading it, |
|
34
|
|
|
* if the entity is not yet loaded. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $id |
|
37
|
|
|
* |
|
38
|
|
|
* @return Proxy|object|null |
|
39
|
|
|
* |
|
40
|
|
|
* @throws \Doctrine\ORM\ORMException |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public function getReference(string $id) |
|
43
|
|
|
{ |
|
44
|
1 |
|
return $this->getEntityManager()->getReference($this->getEntityName(), $id); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Gets all association mappings of the class. |
|
49
|
|
|
* |
|
50
|
|
|
* @return array |
|
51
|
|
|
*/ |
|
52
|
10 |
|
public function getAssociations(): array |
|
53
|
|
|
{ |
|
54
|
10 |
|
return $this->getClassMetaData()->getAssociationMappings(); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return \Doctrine\Common\Persistence\Mapping\ClassMetadata|\Doctrine\ORM\Mapping\ClassMetadata |
|
59
|
|
|
*/ |
|
60
|
10 |
|
public function getClassMetaData() |
|
61
|
|
|
{ |
|
62
|
10 |
|
return $this->getEntityManager()->getClassMetadata($this->getEntityName()); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Getter method for EntityManager for current entity. |
|
67
|
|
|
* |
|
68
|
|
|
* @return EntityManager|ObjectManager |
|
69
|
|
|
*/ |
|
70
|
907 |
|
public function getEntityManager() |
|
71
|
|
|
{ |
|
72
|
907 |
|
return $this->managerRegistry->getManagerForClass(static::$entityName); |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Method to create new query builder for current entity. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $alias |
|
79
|
|
|
* @param string $indexBy |
|
80
|
|
|
* |
|
81
|
|
|
* @return QueryBuilder |
|
82
|
|
|
*/ |
|
83
|
80 |
|
public function createQueryBuilder(string $alias = null, string $indexBy = null): QueryBuilder |
|
84
|
|
|
{ |
|
85
|
80 |
|
$alias = $alias ?? 'entity'; |
|
86
|
|
|
|
|
87
|
|
|
// Create new query builder |
|
88
|
|
|
$queryBuilder = $this |
|
89
|
80 |
|
->getEntityManager() |
|
90
|
80 |
|
->createQueryBuilder() |
|
|
|
|
|
|
91
|
80 |
|
->select($alias) |
|
92
|
80 |
|
->from($this->getEntityName(), $alias, $indexBy); |
|
93
|
|
|
|
|
94
|
80 |
|
return $queryBuilder; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|