|
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
|
|
|
|
|
9
|
|
|
namespace App\Repository\Traits; |
|
10
|
|
|
|
|
11
|
|
|
use App\Rest\UuidHelper; |
|
12
|
|
|
use Doctrine\ORM\EntityManager; |
|
13
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
|
14
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
15
|
|
|
use Ramsey\Uuid\Exception\InvalidUuidStringException; |
|
16
|
|
|
use UnexpectedValueException; |
|
17
|
|
|
use function preg_replace; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class RepositoryWrappersTrait |
|
21
|
|
|
* |
|
22
|
|
|
* @package App\Repository\Traits |
|
23
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
trait RepositoryWrappersTrait |
|
26
|
|
|
{ |
|
27
|
263 |
|
public function getReference(string $id): ?object |
|
28
|
|
|
{ |
|
29
|
|
|
try { |
|
30
|
263 |
|
$referenceId = UuidHelper::fromString($id); |
|
31
|
1 |
|
} catch (InvalidUuidStringException) { |
|
32
|
1 |
|
$referenceId = $id; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
263 |
|
return $this->getEntityManager()->getReference($this->getEntityName(), $referenceId); |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
* |
|
41
|
|
|
* @psalm-return array<string, array<string, mixed>> |
|
42
|
|
|
*/ |
|
43
|
10 |
|
public function getAssociations(): array |
|
44
|
|
|
{ |
|
45
|
10 |
|
return $this->getClassMetaData()->getAssociationMappings(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
11 |
|
public function getClassMetaData(): ClassMetadataInfo |
|
49
|
|
|
{ |
|
50
|
11 |
|
return $this->getEntityManager()->getClassMetadata($this->getEntityName()); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
666 |
|
public function getEntityManager(): EntityManager |
|
54
|
|
|
{ |
|
55
|
666 |
|
$manager = $this->managerRegistry->getManagerForClass($this->getEntityName()); |
|
56
|
|
|
|
|
57
|
666 |
|
if (!($manager instanceof EntityManager)) { |
|
58
|
1 |
|
throw new UnexpectedValueException( |
|
59
|
1 |
|
'Cannot get entity manager for entity \'' . $this->getEntityName() . '\'' |
|
60
|
1 |
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
665 |
|
if ($manager->isOpen() === false) { |
|
64
|
1 |
|
$this->managerRegistry->resetManager(); |
|
65
|
|
|
|
|
66
|
1 |
|
$manager = $this->getEntityManager(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
665 |
|
return $manager; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
356 |
|
public function createQueryBuilder(?string $alias = null, ?string $indexBy = null): QueryBuilder |
|
73
|
|
|
{ |
|
74
|
356 |
|
$alias ??= 'entity'; |
|
75
|
356 |
|
$alias = (string)preg_replace('#[\W]#', '', $alias); |
|
76
|
356 |
|
$indexBy = $indexBy !== null ? (string)preg_replace('#[\W]#', '', $indexBy) : null; |
|
77
|
|
|
|
|
78
|
|
|
// Create new query builder |
|
79
|
356 |
|
return $this |
|
80
|
356 |
|
->getEntityManager() |
|
81
|
356 |
|
->createQueryBuilder() |
|
82
|
356 |
|
->select($alias) |
|
83
|
356 |
|
->from($this->getEntityName(), $alias, $indexBy); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.