1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\ORM\Sequencing\Generator; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Id generator that uses a single-row database table and a hi/lo algorithm. |
11
|
|
|
*/ |
12
|
|
|
class TableGenerator implements Generator |
13
|
|
|
{ |
14
|
|
|
/** @var string */ |
15
|
|
|
private $tableName; |
16
|
|
|
|
17
|
|
|
/** @var string */ |
18
|
|
|
private $sequenceName; |
19
|
|
|
|
20
|
|
|
/** @var int */ |
21
|
|
|
private $allocationSize; |
22
|
|
|
|
23
|
|
|
/** @var int|null */ |
24
|
|
|
private $nextValue; |
25
|
|
|
|
26
|
|
|
/** @var int|null */ |
27
|
|
|
private $maxValue; |
28
|
|
|
|
29
|
|
|
public function __construct(string $tableName, string $sequenceName = 'default', int $allocationSize = 10) |
30
|
|
|
{ |
31
|
|
|
$this->tableName = $tableName; |
32
|
|
|
$this->sequenceName = $sequenceName; |
33
|
|
|
$this->allocationSize = $allocationSize; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public function generate(EntityManagerInterface $em, ?object $entity) |
40
|
|
|
{ |
41
|
|
|
if ($this->maxValue === null || $this->nextValue === $this->maxValue) { |
42
|
|
|
// Allocate new values |
43
|
|
|
$conn = $em->getConnection(); |
44
|
|
|
|
45
|
|
|
if ($conn->getTransactionNestingLevel() === 0) { |
46
|
|
|
// use select for update |
47
|
|
|
$platform = $conn->getDatabasePlatform(); |
48
|
|
|
$sql = $platform->getTableHiLoCurrentValSql($this->tableName, $this->sequenceName); |
|
|
|
|
49
|
|
|
$currentLevel = $conn->fetchColumn($sql); |
50
|
|
|
|
51
|
|
|
if ($currentLevel !== null) { |
52
|
|
|
$this->nextValue = $currentLevel; |
|
|
|
|
53
|
|
|
$this->maxValue = $this->nextValue + $this->allocationSize; |
54
|
|
|
|
55
|
|
|
$updateSql = $platform->getTableHiLoUpdateNextValSql( |
|
|
|
|
56
|
|
|
$this->tableName, |
57
|
|
|
$this->sequenceName, |
58
|
|
|
$this->allocationSize |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
if ($conn->executeUpdate($updateSql, [1 => $currentLevel, 2 => $currentLevel+1]) !== 1) { |
|
|
|
|
62
|
|
|
// no affected rows, concurrency issue, throw exception |
63
|
|
|
} |
64
|
|
|
} else { |
|
|
|
|
65
|
|
|
// no current level returned, TableGenerator seems to be broken, throw exception |
66
|
|
|
} |
67
|
|
|
} else { |
|
|
|
|
68
|
|
|
// only table locks help here, implement this or throw exception? |
69
|
|
|
// or do we want to work with table locks exclusively? |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->nextValue++; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function isPostInsertGenerator() : bool |
80
|
|
|
{ |
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
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.