1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\Numeric; |
4
|
|
|
|
5
|
|
|
// phpcs:disable Generic.Files.LineLength.TooLong |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Types\Type; |
8
|
|
|
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
9
|
|
|
use Doctrine\ORM\Mapping\Builder\FieldBuilder; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\Numeric\IndexedAutoIncrementFieldInterface; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
12
|
|
|
|
13
|
|
|
// phpcs:enable |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* |
17
|
|
|
* ########### WARNING ############ |
18
|
|
|
* |
19
|
|
|
* IF you want to access the auto increment values immediately after persisting the Entity, you will need to flush the |
20
|
|
|
* Entity from the unit of work before loading it from your repository |
21
|
|
|
* |
22
|
|
|
* Eg something like: |
23
|
|
|
* $this->getEntityManager()->getUnitOfWork()->clear(EntityFqn); |
24
|
|
|
* $withAutoIncValueSet=$this->getRepostitory()->loadEntity(); |
25
|
|
|
* |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
trait IndexedAutoIncrementFieldTrait |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
private $indexedAutoIncrement; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
37
|
|
|
* @param ClassMetadataBuilder $builder |
38
|
|
|
*/ |
39
|
|
|
public static function metaForIndexedAutoIncrement(ClassMetadataBuilder $builder): void |
40
|
|
|
{ |
41
|
|
|
$fieldBuilder = new FieldBuilder( |
42
|
|
|
$builder, |
43
|
|
|
[ |
44
|
|
|
'fieldName' => IndexedAutoIncrementFieldInterface::PROP_INDEXED_AUTO_INCREMENT, |
45
|
|
|
'type' => Type::INTEGER, |
46
|
|
|
] |
47
|
|
|
); |
48
|
|
|
$fieldBuilder |
49
|
|
|
->columnName( |
50
|
|
|
MappingHelper::getColumnNameForField( |
51
|
|
|
IndexedAutoIncrementFieldInterface::PROP_INDEXED_AUTO_INCREMENT |
52
|
|
|
) |
53
|
|
|
) |
54
|
|
|
->nullable(false) |
55
|
|
|
->unique(false) |
56
|
|
|
->columnDefinition('INT AUTO_INCREMENT UNIQUE') |
57
|
|
|
->build(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return int |
62
|
|
|
*/ |
63
|
|
|
public function getIndexedAutoIncrement(): ?int |
64
|
|
|
{ |
65
|
|
|
return $this->indexedAutoIncrement; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function initIndexedAutoIncrement() |
69
|
|
|
{ |
70
|
|
|
$this->indexedAutoIncrement = IndexedAutoIncrementFieldInterface::DEFAULT_INDEXED_AUTO_INCREMENT; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|