-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEntityManager.php
More file actions
93 lines (78 loc) · 2.1 KB
/
Copy pathEntityManager.php
File metadata and controls
93 lines (78 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
declare(strict_types=1);
namespace ADT\DoctrineComponents;
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
use Doctrine\ORM\Decorator\EntityManagerDecorator;
use Exception;
use ReflectionClass;
use ReflectionException;
class EntityManager extends EntityManagerDecorator
{
public static bool $isFlushAllowed = true;
/**
* @throws Exception
*/
public function flush(): void
{
if (!self::$isFlushAllowed) {
throw new Exception('You cannot use flush.');
}
// we wrap it into transaction because in onFlush event we can add something to background queue
// in onFlush event, transaction is not started - doctrine starts transaction afterward
// doctrine also starts only 1 transaction and then create save points
$this->beginTransaction();
parent::flush();
$this->commit();
}
public function isPossibleToDeleteEntity(object $entity): bool
{
$bool = true;
$this->beginTransaction();
try {
$this->lowLevelDelete($entity);
} catch (ForeignKeyConstraintViolationException) {
$bool = false;
}
$this->rollback();
return $bool;
}
protected function lowLevelDelete(object $entity): void
{
$class = get_class($entity);
$this->createQueryBuilder()
->delete()
->from($class, 'e')
->andWhere('e = :entity')
->setParameter('entity', $entity)
->getQuery()
->execute();
}
/**
* @throws ReflectionException
* @throws Exception
*/
public function findEntityClassByInterface(string $interfaceName): string
{
foreach ($this->getMetadataFactory()->getAllMetadata() as $classMetadata) {
$className = $classMetadata->getName();
if (new ReflectionClass($className)->implementsInterface($interfaceName)) {
return $className;
}
}
throw new Exception('There is no entity with interface "' . $interfaceName . '".');
}
public function getLock(string $name, int $timeout = -1): void
{
$this->getConnection()->executeStatement(
'SELECT GET_LOCK(?, ?)',
[$name, $timeout]
);
}
public function releaseLock(string $name): void
{
$this->getConnection()->executeStatement(
'SELECT RELEASE_LOCK(?)',
[$name]
);
}
}