Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/ci-mssql.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
on:
- pull_request
- push
pull_request:
push:
branches:
- '*.*'

name: ci-mssql

Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/ci-mysql.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
on:
- pull_request
- push
pull_request:
push:
branches:
- '*.*'

name: ci-mysql

Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/ci-pgsql.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
on:
- pull_request
- push
pull_request:
push:
branches:
- '*.*'

name: ci-pgsql

Expand Down
8 changes: 6 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: build
on:
pull_request:
push:
branches:
- '*.*'

on: [push, pull_request]
name: build

jobs:
test:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"php": ">=8.0",
"psr/event-dispatcher": "^1",
"cycle/orm": "^2.0",
"cycle/schema-builder": "^2.0",
"cycle/schema-builder": "^2.5",
"psr/container": "^1.0|^2.0",
"yiisoft/injector": "^1.0"
},
Expand Down
22 changes: 15 additions & 7 deletions src/Schema/RegistryModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Cycle\ORM\Entity\Behavior\Exception\BehaviorCompilationException;
use Cycle\ORM\Parser\Typecast;
use Cycle\ORM\Parser\TypecastInterface;
use Cycle\ORM\SchemaInterface;
use Cycle\Schema\Defaults;
use Cycle\Schema\Definition\Entity;
use Cycle\Schema\Definition\Field;
use Cycle\Schema\Definition\Map\FieldMap;
Expand Down Expand Up @@ -39,12 +41,14 @@ class RegistryModifier
protected FieldMap $fields;
protected AbstractTable $table;
protected Entity $entity;
protected Defaults $defaults;

public function __construct(Registry $registry, string $role)
{
$this->entity = $registry->getEntity($role);
$this->fields = $this->entity->getFields();
$this->table = $registry->getTableSchema($this->entity);
$this->defaults = $registry->getDefaults();
}

public function addDatetimeColumn(string $columnName, string $fieldName): AbstractColumn
Expand Down Expand Up @@ -135,15 +139,19 @@ public function setTypecast(Field $field, array|string|null $rule, string $handl
$field->setTypecast($rule);
}

$handlers = $this->entity->getTypecast();
if ($handlers === null) {
$this->entity->setTypecast($handler);
return $field;
$defaultHandlers = $this->defaults[SchemaInterface::TYPECAST_HANDLER] ?? [];
if (!\is_array($defaultHandlers)) {
$defaultHandlers = [$defaultHandlers];
}

$handlers = (array) $handlers;
$handlers[] = $handler;
$this->entity->setTypecast(array_unique($handlers));
$handlers = $this->entity->getTypecast() ?? [];
if (!is_array($handlers)) {
$handlers = [$handlers];
}

if (!\in_array($handler, $handlers, true) && !\in_array($handler, $defaultHandlers, true)) {
$this->entity->setTypecast(\array_merge($handlers, [$handler]));
}

return $field;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Cycle\ORM\Entity\Behavior\Tests\Fixtures\CustomTypecast;
use Cycle\ORM\Entity\Behavior\Tests\Functional\Driver\Common\BaseTest;
use Cycle\ORM\Parser\Typecast;
use Cycle\ORM\SchemaInterface;
use Cycle\Schema\Definition\Entity;
use Cycle\Schema\Registry;
use Ramsey\Uuid\Uuid;
Expand Down Expand Up @@ -102,7 +103,7 @@ public function testAddTypecast(): void
);
}

public function testAddCustomTypecast(): void
public function testAddTypecastEntityWithTypecast(): void
{
$this->registry->getEntity(self::ROLE_TEST)->setTypecast(CustomTypecast::class);

Expand All @@ -121,6 +122,25 @@ public function testAddCustomTypecast(): void
);
}

public function testAddTypecastShouldBeSkipped(): void
{
$this->registry->getEntity(self::ROLE_TEST);

$this->modifier->addUuidColumn('uuid_column', 'uuid');
$this->registry->getDefaults()->offsetSet(SchemaInterface::TYPECAST_HANDLER, Typecast::class);

$this->assertNull($this->registry->getEntity(self::ROLE_TEST)->getTypecast());
}

public function testAddTypecastShouldBeDuplicated(): void
{
$this->registry->getEntity(self::ROLE_TEST)->setTypecast(CustomTypecast::class);

$this->modifier->addUuidColumn('uuid_column', 'uuid');

$this->assertSame(CustomTypecast::class, $this->registry->getEntity(self::ROLE_TEST)->getTypecast());
}

public function testCustomTypecastNotOverridden(): void
{
$this->modifier->addUuidColumn('uuid_column', 'uuid');
Expand Down