From ca54ddf8938ef2dd997b40303603db60f61e7f5e Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Tue, 6 Feb 2024 19:40:03 +0200 Subject: [PATCH 1/6] Add generated fields support --- composer.json | 4 +- src/Annotation/Generated.php | 30 +++++++ src/Configurator.php | 30 ++++++- src/Entities.php | 3 + src/Enum/GeneratedType.php | 12 +++ .../Fixtures25/Php82/WithGenerated.php | 29 +++++++ .../Fixtures25/PostgreSQL/WithSerial.php | 24 ++++++ .../Fixtures/Fixtures25/WithGeneratedEnum.php | 29 +++++++ .../Fixtures/Fixtures25/WithGeneratedInt.php | 28 ++++++ .../Driver/Common/GeneratedFieldsTestCase.php | 86 +++++++++++++++++++ .../Inheritance/SingleTableTestCase.php | 3 + .../Driver/MySQL/GeneratedFieldsTest.php | 16 ++++ .../Driver/Postgres/GeneratedFieldsTest.php | 49 +++++++++++ .../Driver/SQLServer/GeneratedFieldsTest.php | 16 ++++ .../Driver/SQLite/GeneratedFieldsTest.php | 16 ++++ 15 files changed, 372 insertions(+), 3 deletions(-) create mode 100644 src/Annotation/Generated.php create mode 100644 src/Enum/GeneratedType.php create mode 100644 tests/Annotated/Fixtures/Fixtures25/Php82/WithGenerated.php create mode 100644 tests/Annotated/Fixtures/Fixtures25/PostgreSQL/WithSerial.php create mode 100644 tests/Annotated/Fixtures/Fixtures25/WithGeneratedEnum.php create mode 100644 tests/Annotated/Fixtures/Fixtures25/WithGeneratedInt.php create mode 100644 tests/Annotated/Functional/Driver/Common/GeneratedFieldsTestCase.php create mode 100644 tests/Annotated/Functional/Driver/MySQL/GeneratedFieldsTest.php create mode 100644 tests/Annotated/Functional/Driver/Postgres/GeneratedFieldsTest.php create mode 100644 tests/Annotated/Functional/Driver/SQLServer/GeneratedFieldsTest.php create mode 100644 tests/Annotated/Functional/Driver/SQLite/GeneratedFieldsTest.php diff --git a/composer.json b/composer.json index d75f7a96..e4ed345c 100644 --- a/composer.json +++ b/composer.json @@ -38,8 +38,8 @@ ], "require": { "php": ">=8.1", - "cycle/orm": "^2.2.0", - "cycle/schema-builder": "^2.6", + "cycle/orm": "^2.7", + "cycle/schema-builder": "^2.8", "spiral/attributes": "^2.8|^3.0", "spiral/tokenizer": "^2.8|^3.0", "doctrine/inflector": "^2.0" diff --git a/src/Annotation/Generated.php b/src/Annotation/Generated.php new file mode 100644 index 00000000..28ca7a94 --- /dev/null +++ b/src/Annotation/Generated.php @@ -0,0 +1,30 @@ +type |= $value instanceof GeneratedType ? $value->value : $value; + } + } + + public function getType(): int + { + return $this->type; + } +} diff --git a/src/Configurator.php b/src/Configurator.php index a679040e..4744c698 100644 --- a/src/Configurator.php +++ b/src/Configurator.php @@ -8,14 +8,16 @@ use Cycle\Annotated\Annotation\Embeddable; use Cycle\Annotated\Annotation\Entity; use Cycle\Annotated\Annotation\ForeignKey; +use Cycle\Annotated\Annotation\Generated; use Cycle\Annotated\Annotation\Relation as RelationAnnotation; +use Cycle\Annotated\Enum\GeneratedType; use Cycle\Annotated\Exception\AnnotationException; use Cycle\Annotated\Exception\AnnotationRequiredArgumentsException; use Cycle\Annotated\Exception\AnnotationWrongTypeArgumentException; use Cycle\Annotated\Utils\EntityUtils; use Cycle\Schema\Definition\Entity as EntitySchema; -use Cycle\Schema\Definition\ForeignKey as ForeignKeySchema; use Cycle\Schema\Definition\Field; +use Cycle\Schema\Definition\ForeignKey as ForeignKeySchema; use Cycle\Schema\Definition\Relation; use Cycle\Schema\Generator\SyncTables; use Cycle\Schema\SchemaModifierInterface; @@ -232,6 +234,9 @@ public function initField(string $name, Column $column, \ReflectionClass $class, $field->setColumn($columnName); $field->setPrimary($column->isPrimary()); + if ($this->isDbGeneratedField($field)) { + $field->setGenerated(GeneratedType::Db->value); + } $field->setTypecast($this->resolveTypecast($column->getTypecast(), $class)); @@ -297,6 +302,20 @@ public function initForeignKeys(Entity $ann, EntitySchema $entity, \ReflectionCl } } + public function initGeneratedFields(EntitySchema $entity, \ReflectionClass $class): void + { + foreach ($class->getProperties() as $property) { + try { + $generated = $this->reader->firstPropertyMetadata($property, Generated::class); + if ($generated !== null) { + $entity->getFields()->get($property->getName())->setGenerated($generated->getType()); + } + } catch (\Throwable $e) { + throw new AnnotationException($e->getMessage(), (int) $e->getCode(), $e); + } + } + } + /** * Resolve class or role name relative to the current class. * @@ -386,4 +405,13 @@ private function getPropertyMetadata(\ReflectionProperty $property, string $name throw new AnnotationException($e->getMessage(), (int) $e->getCode(), $e); } } + + private function isDbGeneratedField(Field $field): bool + { + return $field->isPrimary() + || $field->getType() === 'serial' + || $field->getType() === 'smallserial' + || $field->getType() === 'serial' + || $field->getType() === 'bigserial'; + } } diff --git a/src/Entities.php b/src/Entities.php index bcfca3c0..0e6b8567 100644 --- a/src/Entities.php +++ b/src/Entities.php @@ -77,6 +77,9 @@ public function run(Registry $registry): Registry continue; } + // generated fields + $this->generator->initGeneratedFields($e, $entity->class); + // register entity (OR find parent) $registry->register($e); diff --git a/src/Enum/GeneratedType.php b/src/Enum/GeneratedType.php new file mode 100644 index 00000000..5f50f8d6 --- /dev/null +++ b/src/Enum/GeneratedType.php @@ -0,0 +1,12 @@ +value) + ] + public \DateTimeImmutable $createdAt; + + #[ + Column(type: 'datetime', name: 'updated_at'), + Generated(type: GeneratedType::PhpInsert->value | GeneratedType::PhpUpdate->value) + ] + public \DateTimeImmutable $updatedAt; +} diff --git a/tests/Annotated/Fixtures/Fixtures25/PostgreSQL/WithSerial.php b/tests/Annotated/Fixtures/Fixtures25/PostgreSQL/WithSerial.php new file mode 100644 index 00000000..8081e842 --- /dev/null +++ b/tests/Annotated/Fixtures/Fixtures25/PostgreSQL/WithSerial.php @@ -0,0 +1,24 @@ +reader = new AttributeReader(); + + parent::setUp(); + } + + #[DataProvider('generatedFieldsDataProvider')] + public function testGeneratedFields(string $role): void + { + $tokenizer = new Tokenizer(new TokenizerConfig([ + 'directories' => [__DIR__ . '/../../../Fixtures/Fixtures25'], + 'exclude' => ['Php82', 'PostgreSQL'], + ])); + + $locator = $tokenizer->classLocator(); + + $r = new Registry($this->dbal); + $schema = (new Compiler())->compile($r, [ + new Entities(new TokenizerEntityLocator($locator, $this->reader), $this->reader), + ]); + + $this->assertSame( + [ + 'id' => SchemaInterface::GENERATED_DB, + 'createdAt' => SchemaInterface::GENERATED_PHP_INSERT, + 'updatedAt' => SchemaInterface::GENERATED_PHP_INSERT | SchemaInterface::GENERATED_PHP_UPDATE, + ], + $schema[$role][SchemaInterface::GENERATED_FIELDS] + ); + } + + #[RequiresPhp('^8.2')] + public function testGeneratedFieldsEnumValues(): void + { + $tokenizer = new Tokenizer(new TokenizerConfig([ + 'directories' => [__DIR__ . '/../../../Fixtures/Fixtures25/Php82'], + 'exclude' => [], + ])); + + $locator = $tokenizer->classLocator(); + + $r = new Registry($this->dbal); + + $schema = (new Compiler())->compile($r, [ + new Entities(new TokenizerEntityLocator($locator, $this->reader), $this->reader), + ]); + + $this->assertSame( + [ + 'id' => SchemaInterface::GENERATED_DB, + 'createdAt' => SchemaInterface::GENERATED_PHP_INSERT, + 'updatedAt' => SchemaInterface::GENERATED_PHP_INSERT | SchemaInterface::GENERATED_PHP_UPDATE, + ], + $schema['generatedFieldsEnumValue'][SchemaInterface::GENERATED_FIELDS] + ); + } + + public static function generatedFieldsDataProvider(): \Traversable + { + yield ['generatedFieldsEnum']; + yield ['generatedFieldsInt']; + } +} diff --git a/tests/Annotated/Functional/Driver/Common/Inheritance/SingleTableTestCase.php b/tests/Annotated/Functional/Driver/Common/Inheritance/SingleTableTestCase.php index 67d06cb9..1453ee5a 100644 --- a/tests/Annotated/Functional/Driver/Common/Inheritance/SingleTableTestCase.php +++ b/tests/Annotated/Functional/Driver/Common/Inheritance/SingleTableTestCase.php @@ -306,6 +306,9 @@ public function testSingleTableInheritanceWithDifferentColumnDeclaration( ], SchemaInterface::SCHEMA => [], SchemaInterface::TYPECAST_HANDLER => null, + SchemaInterface::GENERATED_FIELDS => [ + 'id' => SchemaInterface::GENERATED_DB, + ], ], $schema['comment'] ); diff --git a/tests/Annotated/Functional/Driver/MySQL/GeneratedFieldsTest.php b/tests/Annotated/Functional/Driver/MySQL/GeneratedFieldsTest.php new file mode 100644 index 00000000..30f2c995 --- /dev/null +++ b/tests/Annotated/Functional/Driver/MySQL/GeneratedFieldsTest.php @@ -0,0 +1,16 @@ + [__DIR__ . '/../../../Fixtures/Fixtures25/PostgreSQL'], + 'exclude' => [], + ])); + + $locator = $tokenizer->classLocator(); + + $r = new Registry($this->dbal); + + $schema = (new Compiler())->compile($r, [ + new Entities(new TokenizerEntityLocator($locator, $this->reader), $this->reader), + ]); + + $this->assertSame( + [ + 'id' => SchemaInterface::GENERATED_DB, + 'smallSerial' => SchemaInterface::GENERATED_DB, + 'serial' => SchemaInterface::GENERATED_DB, + 'bigSerial' => SchemaInterface::GENERATED_DB, + ], + $schema['generatedFieldsSerial'][SchemaInterface::GENERATED_FIELDS] + ); + } +} diff --git a/tests/Annotated/Functional/Driver/SQLServer/GeneratedFieldsTest.php b/tests/Annotated/Functional/Driver/SQLServer/GeneratedFieldsTest.php new file mode 100644 index 00000000..3c3759d2 --- /dev/null +++ b/tests/Annotated/Functional/Driver/SQLServer/GeneratedFieldsTest.php @@ -0,0 +1,16 @@ + Date: Thu, 8 Feb 2024 22:17:33 +0200 Subject: [PATCH 2/6] Remove enum, rename constants --- src/Annotation/Generated.php | 31 ++++++---- src/Configurator.php | 8 +-- src/Enum/GeneratedType.php | 12 ---- .../Fixtures25/Php82/WithGenerated.php | 29 --------- ...WithSerial.php => WithGeneratedSerial.php} | 19 +++++- .../Fixtures/Fixtures25/WithGeneratedEnum.php | 29 --------- .../Fixtures25/WithGeneratedFields.php | 52 ++++++++++++++++ .../Fixtures/Fixtures25/WithGeneratedInt.php | 28 --------- .../Driver/Common/GeneratedFieldsTestCase.php | 61 +++---------------- .../Inheritance/SingleTableTestCase.php | 3 +- .../Driver/Postgres/GeneratedFieldsTest.php | 18 +++--- 11 files changed, 115 insertions(+), 175 deletions(-) delete mode 100644 src/Enum/GeneratedType.php delete mode 100644 tests/Annotated/Fixtures/Fixtures25/Php82/WithGenerated.php rename tests/Annotated/Fixtures/Fixtures25/PostgreSQL/{WithSerial.php => WithGeneratedSerial.php} (52%) delete mode 100644 tests/Annotated/Fixtures/Fixtures25/WithGeneratedEnum.php create mode 100644 tests/Annotated/Fixtures/Fixtures25/WithGeneratedFields.php delete mode 100644 tests/Annotated/Fixtures/Fixtures25/WithGeneratedInt.php diff --git a/src/Annotation/Generated.php b/src/Annotation/Generated.php index 28ca7a94..d663d215 100644 --- a/src/Annotation/Generated.php +++ b/src/Annotation/Generated.php @@ -4,27 +4,34 @@ namespace Cycle\Annotated\Annotation; -use Cycle\Annotated\Enum\GeneratedType; +use Cycle\ORM\Schema\GeneratedField; use Spiral\Attributes\NamedArgumentConstructor; +/** + * @Annotation + * @NamedArgumentConstructor + * @Target({"PROPERTY"}) + */ #[\Attribute(\Attribute::TARGET_PROPERTY)] #[NamedArgumentConstructor] class Generated { - protected int $type = 0; + public function __construct( + protected bool $beforeInsert = false, + protected bool $onInsert = false, + protected bool $beforeUpdate = false, + ) { + } - /** - * @param GeneratedType|int ...$type Generating type {@see GeneratedType}. - */ - public function __construct(GeneratedType|int ...$type) + public function getType(): ?int { - foreach ($type as $value) { - $this->type |= $value instanceof GeneratedType ? $value->value : $value; + if (!$this->beforeInsert && !$this->onInsert && !$this->beforeUpdate) { + return null; } - } - public function getType(): int - { - return $this->type; + return + ($this->beforeInsert ? GeneratedField::BEFORE_INSERT : 0) | + ($this->onInsert ? GeneratedField::ON_INSERT : 0) | + ($this->beforeUpdate ? GeneratedField::BEFORE_UPDATE : 0); } } diff --git a/src/Configurator.php b/src/Configurator.php index 4744c698..ac385ca7 100644 --- a/src/Configurator.php +++ b/src/Configurator.php @@ -10,11 +10,11 @@ use Cycle\Annotated\Annotation\ForeignKey; use Cycle\Annotated\Annotation\Generated; use Cycle\Annotated\Annotation\Relation as RelationAnnotation; -use Cycle\Annotated\Enum\GeneratedType; use Cycle\Annotated\Exception\AnnotationException; use Cycle\Annotated\Exception\AnnotationRequiredArgumentsException; use Cycle\Annotated\Exception\AnnotationWrongTypeArgumentException; use Cycle\Annotated\Utils\EntityUtils; +use Cycle\ORM\Schema\GeneratedField; use Cycle\Schema\Definition\Entity as EntitySchema; use Cycle\Schema\Definition\Field; use Cycle\Schema\Definition\ForeignKey as ForeignKeySchema; @@ -234,8 +234,8 @@ public function initField(string $name, Column $column, \ReflectionClass $class, $field->setColumn($columnName); $field->setPrimary($column->isPrimary()); - if ($this->isDbGeneratedField($field)) { - $field->setGenerated(GeneratedType::Db->value); + if ($this->isOnInsertGeneratedField($field)) { + $field->setGenerated(GeneratedField::ON_INSERT); } $field->setTypecast($this->resolveTypecast($column->getTypecast(), $class)); @@ -406,7 +406,7 @@ private function getPropertyMetadata(\ReflectionProperty $property, string $name } } - private function isDbGeneratedField(Field $field): bool + private function isOnInsertGeneratedField(Field $field): bool { return $field->isPrimary() || $field->getType() === 'serial' diff --git a/src/Enum/GeneratedType.php b/src/Enum/GeneratedType.php deleted file mode 100644 index 5f50f8d6..00000000 --- a/src/Enum/GeneratedType.php +++ /dev/null @@ -1,12 +0,0 @@ -value) - ] - public \DateTimeImmutable $createdAt; - - #[ - Column(type: 'datetime', name: 'updated_at'), - Generated(type: GeneratedType::PhpInsert->value | GeneratedType::PhpUpdate->value) - ] - public \DateTimeImmutable $updatedAt; -} diff --git a/tests/Annotated/Fixtures/Fixtures25/PostgreSQL/WithSerial.php b/tests/Annotated/Fixtures/Fixtures25/PostgreSQL/WithGeneratedSerial.php similarity index 52% rename from tests/Annotated/Fixtures/Fixtures25/PostgreSQL/WithSerial.php rename to tests/Annotated/Fixtures/Fixtures25/PostgreSQL/WithGeneratedSerial.php index 8081e842..bfe0e4f7 100644 --- a/tests/Annotated/Fixtures/Fixtures25/PostgreSQL/WithSerial.php +++ b/tests/Annotated/Fixtures/Fixtures25/PostgreSQL/WithGeneratedSerial.php @@ -7,18 +7,33 @@ use Cycle\Annotated\Annotation\Column; use Cycle\Annotated\Annotation\Entity; -#[Entity(role: 'generatedFieldsSerial', table: 'generated_fields_serial')] -class WithSerial +/** + * @Entity(role="withGeneratedSerial", table="with_generated_serial") + */ +#[Entity(role: 'withGeneratedSerial', table: 'with_generated_serial')] +class WithGeneratedSerial { + /** + * @Column(type="primary") + */ #[Column(type: 'primary')] public int $id; + /** + * @Column(type="smallserial", name="small_serial") + */ #[Column(type: 'smallserial', name: 'small_serial')] public int $smallSerial; + /** + * @Column(type="serial") + */ #[Column(type: 'serial')] public int $serial; + /** + * @Column(type="bigserial", name="big_serial") + */ #[Column(type: 'bigserial', name: 'big_serial')] public int $bigSerial; } diff --git a/tests/Annotated/Fixtures/Fixtures25/WithGeneratedEnum.php b/tests/Annotated/Fixtures/Fixtures25/WithGeneratedEnum.php deleted file mode 100644 index 72bcce8d..00000000 --- a/tests/Annotated/Fixtures/Fixtures25/WithGeneratedEnum.php +++ /dev/null @@ -1,29 +0,0 @@ -reader = new AttributeReader(); - - parent::setUp(); - } - - #[DataProvider('generatedFieldsDataProvider')] - public function testGeneratedFields(string $role): void + #[DataProvider('allReadersProvider')] + public function testGeneratedFields(ReaderInterface $reader): void { $tokenizer = new Tokenizer(new TokenizerConfig([ 'directories' => [__DIR__ . '/../../../Fixtures/Fixtures25'], - 'exclude' => ['Php82', 'PostgreSQL'], + 'exclude' => ['PostgreSQL'], ])); $locator = $tokenizer->classLocator(); $r = new Registry($this->dbal); $schema = (new Compiler())->compile($r, [ - new Entities(new TokenizerEntityLocator($locator, $this->reader), $this->reader), + new Entities(new TokenizerEntityLocator($locator, $reader), $reader), ]); $this->assertSame( [ - 'id' => SchemaInterface::GENERATED_DB, - 'createdAt' => SchemaInterface::GENERATED_PHP_INSERT, - 'updatedAt' => SchemaInterface::GENERATED_PHP_INSERT | SchemaInterface::GENERATED_PHP_UPDATE, + 'id' => GeneratedField::ON_INSERT, + 'createdAt' => GeneratedField::BEFORE_INSERT, + 'createdAtGeneratedByDatabase' => GeneratedField::ON_INSERT, + 'updatedAt' => GeneratedField::BEFORE_INSERT | GeneratedField::BEFORE_UPDATE, ], - $schema[$role][SchemaInterface::GENERATED_FIELDS] + $schema['withGeneratedFields'][SchemaInterface::GENERATED_FIELDS] ); } - - #[RequiresPhp('^8.2')] - public function testGeneratedFieldsEnumValues(): void - { - $tokenizer = new Tokenizer(new TokenizerConfig([ - 'directories' => [__DIR__ . '/../../../Fixtures/Fixtures25/Php82'], - 'exclude' => [], - ])); - - $locator = $tokenizer->classLocator(); - - $r = new Registry($this->dbal); - - $schema = (new Compiler())->compile($r, [ - new Entities(new TokenizerEntityLocator($locator, $this->reader), $this->reader), - ]); - - $this->assertSame( - [ - 'id' => SchemaInterface::GENERATED_DB, - 'createdAt' => SchemaInterface::GENERATED_PHP_INSERT, - 'updatedAt' => SchemaInterface::GENERATED_PHP_INSERT | SchemaInterface::GENERATED_PHP_UPDATE, - ], - $schema['generatedFieldsEnumValue'][SchemaInterface::GENERATED_FIELDS] - ); - } - - public static function generatedFieldsDataProvider(): \Traversable - { - yield ['generatedFieldsEnum']; - yield ['generatedFieldsInt']; - } } diff --git a/tests/Annotated/Functional/Driver/Common/Inheritance/SingleTableTestCase.php b/tests/Annotated/Functional/Driver/Common/Inheritance/SingleTableTestCase.php index 1453ee5a..bd620e8b 100644 --- a/tests/Annotated/Functional/Driver/Common/Inheritance/SingleTableTestCase.php +++ b/tests/Annotated/Functional/Driver/Common/Inheritance/SingleTableTestCase.php @@ -21,6 +21,7 @@ use Cycle\ORM\Relation; use Cycle\ORM\Schema; use Cycle\ORM\SchemaInterface; +use Cycle\ORM\Schema\GeneratedField; use Cycle\ORM\Select\Repository; use Cycle\ORM\Select\Source; use Cycle\ORM\EntityManager; @@ -307,7 +308,7 @@ public function testSingleTableInheritanceWithDifferentColumnDeclaration( SchemaInterface::SCHEMA => [], SchemaInterface::TYPECAST_HANDLER => null, SchemaInterface::GENERATED_FIELDS => [ - 'id' => SchemaInterface::GENERATED_DB, + 'id' => GeneratedField::ON_INSERT, ], ], $schema['comment'] diff --git a/tests/Annotated/Functional/Driver/Postgres/GeneratedFieldsTest.php b/tests/Annotated/Functional/Driver/Postgres/GeneratedFieldsTest.php index bd9e01de..d0fb3376 100644 --- a/tests/Annotated/Functional/Driver/Postgres/GeneratedFieldsTest.php +++ b/tests/Annotated/Functional/Driver/Postgres/GeneratedFieldsTest.php @@ -8,10 +8,13 @@ use Cycle\Annotated\Entities; use Cycle\Annotated\Locator\TokenizerEntityLocator; use Cycle\Annotated\Tests\Functional\Driver\Common\GeneratedFieldsTestCase; +use Cycle\ORM\Schema\GeneratedField; use Cycle\ORM\SchemaInterface; use Cycle\Schema\Compiler; use Cycle\Schema\Registry; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; +use Spiral\Attributes\ReaderInterface; use Spiral\Tokenizer\Config\TokenizerConfig; use Spiral\Tokenizer\Tokenizer; @@ -21,7 +24,8 @@ final class GeneratedFieldsTest extends GeneratedFieldsTestCase { public const DRIVER = 'postgres'; - public function testSerialGeneratedFields(): void + #[DataProvider('allReadersProvider')] + public function testSerialGeneratedFields(ReaderInterface $reader): void { $tokenizer = new Tokenizer(new TokenizerConfig([ 'directories' => [__DIR__ . '/../../../Fixtures/Fixtures25/PostgreSQL'], @@ -33,17 +37,17 @@ public function testSerialGeneratedFields(): void $r = new Registry($this->dbal); $schema = (new Compiler())->compile($r, [ - new Entities(new TokenizerEntityLocator($locator, $this->reader), $this->reader), + new Entities(new TokenizerEntityLocator($locator, $reader), $reader), ]); $this->assertSame( [ - 'id' => SchemaInterface::GENERATED_DB, - 'smallSerial' => SchemaInterface::GENERATED_DB, - 'serial' => SchemaInterface::GENERATED_DB, - 'bigSerial' => SchemaInterface::GENERATED_DB, + 'id' => GeneratedField::ON_INSERT, + 'smallSerial' => GeneratedField::ON_INSERT, + 'serial' => GeneratedField::ON_INSERT, + 'bigSerial' => GeneratedField::ON_INSERT, ], - $schema['generatedFieldsSerial'][SchemaInterface::GENERATED_FIELDS] + $schema['withGeneratedSerial'][SchemaInterface::GENERATED_FIELDS] ); } } From eebc0126c21e4ee26944f37b31988d684e7d54b8 Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Thu, 8 Feb 2024 22:21:19 +0200 Subject: [PATCH 3/6] Use match --- src/Configurator.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Configurator.php b/src/Configurator.php index ac385ca7..8bdda3bf 100644 --- a/src/Configurator.php +++ b/src/Configurator.php @@ -408,10 +408,8 @@ private function getPropertyMetadata(\ReflectionProperty $property, string $name private function isOnInsertGeneratedField(Field $field): bool { - return $field->isPrimary() - || $field->getType() === 'serial' - || $field->getType() === 'smallserial' - || $field->getType() === 'serial' - || $field->getType() === 'bigserial'; + return $field->isPrimary() || match ($field->getType()) { + 'serial', 'bigserial', 'smallserial' => true + }; } } From 792de93e6e07d69386a7a4a6a99b8b2a91c63cfd Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Thu, 8 Feb 2024 22:29:13 +0200 Subject: [PATCH 4/6] Fix match --- src/Configurator.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Configurator.php b/src/Configurator.php index 8bdda3bf..f22e5381 100644 --- a/src/Configurator.php +++ b/src/Configurator.php @@ -408,8 +408,9 @@ private function getPropertyMetadata(\ReflectionProperty $property, string $name private function isOnInsertGeneratedField(Field $field): bool { - return $field->isPrimary() || match ($field->getType()) { - 'serial', 'bigserial', 'smallserial' => true + return match ($field->getType()) { + 'serial', 'bigserial', 'smallserial' => true, + default => $field->isPrimary() }; } } From feb91a222c1cd05ec629a72ed4484a724b903f85 Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Thu, 8 Feb 2024 22:37:23 +0200 Subject: [PATCH 5/6] Rename method from getType to getFlags --- src/Annotation/Generated.php | 2 +- src/Configurator.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Annotation/Generated.php b/src/Annotation/Generated.php index d663d215..c27bc013 100644 --- a/src/Annotation/Generated.php +++ b/src/Annotation/Generated.php @@ -23,7 +23,7 @@ public function __construct( ) { } - public function getType(): ?int + public function getFlags(): ?int { if (!$this->beforeInsert && !$this->onInsert && !$this->beforeUpdate) { return null; diff --git a/src/Configurator.php b/src/Configurator.php index f22e5381..ea30c586 100644 --- a/src/Configurator.php +++ b/src/Configurator.php @@ -308,7 +308,7 @@ public function initGeneratedFields(EntitySchema $entity, \ReflectionClass $clas try { $generated = $this->reader->firstPropertyMetadata($property, Generated::class); if ($generated !== null) { - $entity->getFields()->get($property->getName())->setGenerated($generated->getType()); + $entity->getFields()->get($property->getName())->setGenerated($generated->getFlags()); } } catch (\Throwable $e) { throw new AnnotationException($e->getMessage(), (int) $e->getCode(), $e); From f603c1113c21df1350ce7d743042559796534f57 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Fri, 9 Feb 2024 01:41:17 +0400 Subject: [PATCH 6/6] Rename attribute to GeneratedValue --- .../{Generated.php => GeneratedValue.php} | 2 +- src/Configurator.php | 4 ++-- .../Fixtures/Fixtures25/WithGeneratedFields.php | 14 +++++++------- 3 files changed, 10 insertions(+), 10 deletions(-) rename src/Annotation/{Generated.php => GeneratedValue.php} (97%) diff --git a/src/Annotation/Generated.php b/src/Annotation/GeneratedValue.php similarity index 97% rename from src/Annotation/Generated.php rename to src/Annotation/GeneratedValue.php index c27bc013..8c108a1f 100644 --- a/src/Annotation/Generated.php +++ b/src/Annotation/GeneratedValue.php @@ -14,7 +14,7 @@ */ #[\Attribute(\Attribute::TARGET_PROPERTY)] #[NamedArgumentConstructor] -class Generated +class GeneratedValue { public function __construct( protected bool $beforeInsert = false, diff --git a/src/Configurator.php b/src/Configurator.php index ea30c586..e5b06ebe 100644 --- a/src/Configurator.php +++ b/src/Configurator.php @@ -8,7 +8,7 @@ use Cycle\Annotated\Annotation\Embeddable; use Cycle\Annotated\Annotation\Entity; use Cycle\Annotated\Annotation\ForeignKey; -use Cycle\Annotated\Annotation\Generated; +use Cycle\Annotated\Annotation\GeneratedValue; use Cycle\Annotated\Annotation\Relation as RelationAnnotation; use Cycle\Annotated\Exception\AnnotationException; use Cycle\Annotated\Exception\AnnotationRequiredArgumentsException; @@ -306,7 +306,7 @@ public function initGeneratedFields(EntitySchema $entity, \ReflectionClass $clas { foreach ($class->getProperties() as $property) { try { - $generated = $this->reader->firstPropertyMetadata($property, Generated::class); + $generated = $this->reader->firstPropertyMetadata($property, GeneratedValue::class); if ($generated !== null) { $entity->getFields()->get($property->getName())->setGenerated($generated->getFlags()); } diff --git a/tests/Annotated/Fixtures/Fixtures25/WithGeneratedFields.php b/tests/Annotated/Fixtures/Fixtures25/WithGeneratedFields.php index 07585654..27e5d985 100644 --- a/tests/Annotated/Fixtures/Fixtures25/WithGeneratedFields.php +++ b/tests/Annotated/Fixtures/Fixtures25/WithGeneratedFields.php @@ -6,7 +6,7 @@ use Cycle\Annotated\Annotation\Column; use Cycle\Annotated\Annotation\Entity; -use Cycle\Annotated\Annotation\Generated; +use Cycle\Annotated\Annotation\GeneratedValue; /** * @Entity(role="withGeneratedFields", table="with_generated_fields") @@ -22,31 +22,31 @@ class WithGeneratedFields /** * @Column(type="datetime", name="created_at") - * @Generated(beforeInsert=true) + * @GeneratedValue(beforeInsert=true) */ #[ Column(type: 'datetime', name: 'created_at'), - Generated(beforeInsert: true) + GeneratedValue(beforeInsert: true) ] public \DateTimeImmutable $createdAt; /** * @Column(type="datetime", name="created_at_generated_by_database") - * @Generated(onInsert=true) + * @GeneratedValue(onInsert=true) */ #[ Column(type: 'datetime', name: 'created_at_generated_by_database'), - Generated(onInsert: true) + GeneratedValue(onInsert: true) ] public \DateTimeImmutable $createdAtGeneratedByDatabase; /** * @Column(type="datetime", name="created_at") - * @Generated(beforeInsert=true, beforeUpdate=true) + * @GeneratedValue(beforeInsert=true, beforeUpdate=true) */ #[ Column(type: 'datetime', name: 'updated_at'), - Generated(beforeInsert: true, beforeUpdate: true) + GeneratedValue(beforeInsert: true, beforeUpdate: true) ] public \DateTimeImmutable $updatedAt; }