diff --git a/src/Command/Database/Insert.php b/src/Command/Database/Insert.php index 4a35846b8..0518f041d 100644 --- a/src/Command/Database/Insert.php +++ b/src/Command/Database/Insert.php @@ -11,6 +11,7 @@ use Cycle\ORM\Command\Traits\MapperTrait; use Cycle\ORM\Heap\State; use Cycle\ORM\MapperInterface; +use Cycle\ORM\Schema\GeneratedField; use Cycle\ORM\SchemaInterface; /** @@ -87,7 +88,7 @@ public function execute(): void } // unset db-generated fields if they are null foreach ($this->generated as $column => $mode) { - if (($mode & SchemaInterface::GENERATED_DB) === 0x0) { + if (($mode & GeneratedField::DB_INSERT) === 0x0) { continue; } @@ -164,7 +165,7 @@ private function hasGeneratedFields(): bool $data = $this->state->getData(); foreach ($this->generated as $field => $mode) { - if (($mode & (SchemaInterface::GENERATED_DB | SchemaInterface::GENERATED_PHP_INSERT)) === 0x0) { + if (($mode & (GeneratedField::DB_INSERT | GeneratedField::PHP_INSERT)) === 0x0) { continue; } diff --git a/src/Heap/Traits/WaitFieldTrait.php b/src/Heap/Traits/WaitFieldTrait.php index 695b6be19..b985c1d92 100644 --- a/src/Heap/Traits/WaitFieldTrait.php +++ b/src/Heap/Traits/WaitFieldTrait.php @@ -18,7 +18,7 @@ public function waitField(string $key, bool $required = true): void public function getWaitingFields(bool $requiredOnly = false): array { - return array_keys($requiredOnly ? array_filter($this->waitingFields) : $this->waitingFields); + return \array_keys($requiredOnly ? \array_filter($this->waitingFields) : $this->waitingFields); } /** diff --git a/src/Relation/BelongsTo.php b/src/Relation/BelongsTo.php index 014071a54..0517ecc0b 100644 --- a/src/Relation/BelongsTo.php +++ b/src/Relation/BelongsTo.php @@ -179,7 +179,7 @@ private function checkNullValuePossibility(Tuple $tuple): bool } if ($tuple->status < Tuple::STATUS_PREPROCESSED - && array_intersect($this->innerKeys, $tuple->state->getWaitingFields(false)) !== [] + && \array_intersect($this->innerKeys, $tuple->state->getWaitingFields(false)) !== [] ) { return true; } diff --git a/src/Relation/RefersTo.php b/src/Relation/RefersTo.php index a150fdee8..9858cd806 100644 --- a/src/Relation/RefersTo.php +++ b/src/Relation/RefersTo.php @@ -98,7 +98,7 @@ public function queue(Pool $pool, Tuple $tuple): void if ($rTuple->status === Tuple::STATUS_PROCESSED || ($rTuple->status > Tuple::STATUS_PREPARING && $rTuple->state->getStatus() !== node::NEW - && array_intersect($this->outerKeys, $rTuple->state->getWaitingFields()) === []) + && \array_intersect($this->outerKeys, $rTuple->state->getWaitingFields()) === []) ) { $this->pullValues($tuple->state, $rTuple->state); $node->setRelation($this->getName(), $related); diff --git a/src/Schema/GeneratedField.php b/src/Schema/GeneratedField.php new file mode 100644 index 000000000..c188bcd02 --- /dev/null +++ b/src/Schema/GeneratedField.php @@ -0,0 +1,38 @@ + generating type] - - public const GENERATED_DB = 1; // sequences and others - public const GENERATED_PHP_INSERT = 2; // generated by PHP code on insert like uuid - public const GENERATED_PHP_UPDATE = 4; // generated by PHP code on update like time - /** * Return all roles defined within the schema. */ diff --git a/src/Transaction/Runner.php b/src/Transaction/Runner.php index 224396df8..2083f15ec 100644 --- a/src/Transaction/Runner.php +++ b/src/Transaction/Runner.php @@ -75,7 +75,7 @@ public function complete(): void { if ($this->mode === self::MODE_OPEN_TRANSACTION) { // Commit all of the open and normalized database transactions - foreach (array_reverse($this->drivers) as $driver) { + foreach (\array_reverse($this->drivers) as $driver) { /** @var DriverInterface $driver */ $driver->commitTransaction(); } @@ -94,14 +94,14 @@ public function rollback(): void { if ($this->mode === self::MODE_OPEN_TRANSACTION) { // Close all open and normalized database transactions - foreach (array_reverse($this->drivers) as $driver) { + foreach (\array_reverse($this->drivers) as $driver) { /** @var DriverInterface $driver */ $driver->rollbackTransaction(); } } // Close all of external types of transactions (revert changes) - foreach (array_reverse($this->executed) as $command) { + foreach (\array_reverse($this->executed) as $command) { if ($command instanceof RollbackMethodInterface) { $command->rollBack(); } @@ -145,7 +145,7 @@ private function prepareTransaction(DriverInterface $driver): void if ($this->mode === self::MODE_CONTINUE_TRANSACTION) { if ($driver->getTransactionLevel() === 0) { - throw new RunnerException(sprintf( + throw new RunnerException(\sprintf( 'The `%s` driver connection has no opened transaction.', $driver->getType() )); diff --git a/src/Transaction/UnitOfWork.php b/src/Transaction/UnitOfWork.php index 9ebbab7f3..9f4a2e557 100644 --- a/src/Transaction/UnitOfWork.php +++ b/src/Transaction/UnitOfWork.php @@ -265,7 +265,7 @@ private function resolveSlaveRelations(Tuple $tuple, RelationMap $map): int if (!$map->hasSlaves()) { return self::RELATIONS_RESOLVED; } - $changedFields = array_keys($tuple->state->getChanges()); + $changedFields = \array_keys($tuple->state->getChanges()); // Attach children to pool $transactData = $tuple->state->getTransactionData(); @@ -281,15 +281,15 @@ private function resolveSlaveRelations(Tuple $tuple, RelationMap $map): int } $innerKeys = $relation->getInnerKeys(); - $isWaitingKeys = array_intersect($innerKeys, $tuple->state->getWaitingFields(true)) !== []; - $hasChangedKeys = array_intersect($innerKeys, $changedFields) !== []; + $isWaitingKeys = \array_intersect($innerKeys, $tuple->state->getWaitingFields(true)) !== []; + $hasChangedKeys = \array_intersect($innerKeys, $changedFields) !== []; if ($relationStatus === RelationInterface::STATUS_PREPARE) { $relData ??= $tuple->mapper->fetchRelations($tuple->entity); $relation->prepare( $this->pool, $tuple, $relData[$name] ?? null, - $isWaitingKeys || $hasChangedKeys + $isWaitingKeys || $hasChangedKeys, ); $relationStatus = $tuple->state->getRelationStatus($relation->getName()); } @@ -298,9 +298,9 @@ private function resolveSlaveRelations(Tuple $tuple, RelationMap $map): int && $relationStatus !== RelationInterface::STATUS_RESOLVED && !$isWaitingKeys && !$hasChangedKeys - && \count(array_intersect($innerKeys, array_keys($transactData))) === \count($innerKeys) + && \count(\array_intersect($innerKeys, \array_keys($transactData))) === \count($innerKeys) ) { - $child ??= $tuple->state->getRelation($name); + // $child ??= $tuple->state->getRelation($name); $relation->queue($this->pool, $tuple); $relationStatus = $tuple->state->getRelationStatus($relation->getName()); } @@ -316,7 +316,7 @@ private function resolveSelfWithEmbedded(Tuple $tuple, RelationMap $map, bool $h if (!$map->hasEmbedded() && !$tuple->state->hasChanges()) { $tuple->status = !$hasDeferredRelations ? Tuple::STATUS_PROCESSED - : max(Tuple::STATUS_DEFERRED, $tuple->status); + : \max(Tuple::STATUS_DEFERRED, $tuple->status); return; } @@ -358,7 +358,6 @@ private function resolveRelations(Tuple $tuple): void { $map = $this->orm->getRelationMap($tuple->node->getRole()); - // Dependency relations $result = $tuple->task === Tuple::TASK_STORE ? $this->resolveMasterRelations($tuple, $map) : $this->resolveSlaveRelations($tuple, $map); @@ -368,8 +367,8 @@ private function resolveRelations(Tuple $tuple): void // Self if ($deferred && $tuple->status < Tuple::STATUS_PROPOSED) { $tuple->status = Tuple::STATUS_DEFERRED; - // $this->pool->attachTuple($tuple); } + if ($isDependenciesResolved) { if ($tuple->task === Tuple::TASK_STORE) { $this->resolveSelfWithEmbedded($tuple, $map, $deferred); @@ -383,7 +382,6 @@ private function resolveRelations(Tuple $tuple): void } if ($tuple->cascade) { - // Slave relations relations $tuple->task === Tuple::TASK_STORE ? $this->resolveSlaveRelations($tuple, $map) : $this->resolveMasterRelations($tuple, $map); diff --git a/tests/ORM/Functional/Driver/Common/Integration/Case321/schema.php b/tests/ORM/Functional/Driver/Common/Integration/Case321/schema.php index a473fbf74..98a8c8008 100644 --- a/tests/ORM/Functional/Driver/Common/Integration/Case321/schema.php +++ b/tests/ORM/Functional/Driver/Common/Integration/Case321/schema.php @@ -3,6 +3,7 @@ declare(strict_types=1); use Cycle\ORM\Mapper\Mapper; +use Cycle\ORM\Schema\GeneratedField; use Cycle\ORM\SchemaInterface as Schema; use Cycle\ORM\Select\Source; use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case321\Entity\User1; @@ -27,7 +28,7 @@ ], Schema::SCHEMA => [], Schema::GENERATED_FIELDS => [ - 'id' => Schema::GENERATED_DB, // autoincrement + 'id' => GeneratedField::DB_INSERT, // autoincrement ], ], 'user2' => [ @@ -48,7 +49,7 @@ ], Schema::SCHEMA => [], Schema::GENERATED_FIELDS => [ - 'id' => Schema::GENERATED_DB, // autoincrement + 'id' => GeneratedField::DB_INSERT, // autoincrement ], ], ]; diff --git a/tests/ORM/Functional/Driver/Postgres/SerialColumnTest.php b/tests/ORM/Functional/Driver/Postgres/SerialColumnTest.php index 85ba2fe7f..8ecac5819 100644 --- a/tests/ORM/Functional/Driver/Postgres/SerialColumnTest.php +++ b/tests/ORM/Functional/Driver/Postgres/SerialColumnTest.php @@ -9,6 +9,7 @@ use Cycle\ORM\Mapper\Mapper; use Cycle\ORM\ORMInterface; use Cycle\ORM\Schema; +use Cycle\ORM\Schema\GeneratedField; use Cycle\ORM\SchemaInterface; use Cycle\ORM\Select; use Cycle\ORM\Tests\Fixtures\CyclicRef2\Document; @@ -64,7 +65,7 @@ public function setUp(): void SchemaInterface::SCHEMA => [], SchemaInterface::RELATIONS => [], SchemaInterface::GENERATED_FIELDS => [ - 'balance' => SchemaInterface::GENERATED_DB, // sequence + 'balance' => GeneratedField::DB_INSERT, // sequence ], ], Document::class => [ @@ -81,10 +82,10 @@ public function setUp(): void 'updated_at' => 'datetime', ], SchemaInterface::GENERATED_FIELDS => [ - 'id' => SchemaInterface::GENERATED_DB, - 'body' => SchemaInterface::GENERATED_DB, - 'created_at' => SchemaInterface::GENERATED_DB, - 'updated_at' => SchemaInterface::GENERATED_PHP_INSERT | SchemaInterface::GENERATED_PHP_UPDATE, + 'id' => GeneratedField::DB_INSERT, + 'body' => GeneratedField::DB_INSERT, + 'created_at' => GeneratedField::DB_INSERT, + 'updated_at' => GeneratedField::PHP_INSERT | GeneratedField::PHP_UPDATE, ], ], ])); @@ -134,7 +135,7 @@ public function testPersistMultipleSerial(): void protected function getCommandGenerator(): ?Transaction\CommandGeneratorInterface { - return new class extends Transaction\CommandGenerator { + return new class () extends Transaction\CommandGenerator { protected function storeEntity(ORMInterface $orm, Transaction\Tuple $tuple, bool $isNew): ?CommandInterface { /** @var CommandInterface|null $command */