Skip to content

Commit

Permalink
Fix Migrator with joined/imported fields (#1227)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Voříšek <mvorisek@mvorisek.cz>
  • Loading branch information
PhilippGrashoff and mvorisek authored Aug 5, 2024
1 parent d62db98 commit 7fb3edf
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
14 changes: 9 additions & 5 deletions src/Schema/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,27 +290,31 @@ public function setModel(Model $model): Model
$this->table($model->table);

foreach ($model->getFields() as $field) {
if ($field->neverPersist || $field instanceof SqlExpressionField) {
if (
$field->neverPersist
|| $field->hasJoin()
|| $field instanceof SqlExpressionField
) {
continue;
}

if ($field->shortName === $model->idField) {
$refype = self::REF_TYPE_PRIMARY;
$refType = self::REF_TYPE_PRIMARY;
$persistField = $field;
} else {
$refField = $field->hasReference() ? $this->getReferenceField($field) : null;
if ($refField !== null) {
$refype = self::REF_TYPE_LINK;
$refType = self::REF_TYPE_LINK;
$persistField = $refField;
} else {
$refype = self::REF_TYPE_NONE;
$refType = self::REF_TYPE_NONE;
$persistField = $field;
}
}

$options = [
'type' => $persistField->type,
'ref_type' => $refype,
'ref_type' => $refType,
'nullable' => ($field->nullable && !$field->required) || ($persistField->nullable && !$persistField->required),
];

Expand Down
48 changes: 47 additions & 1 deletion tests/Schema/MigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,14 @@ public function testCreateSetModel(): void
$user->createEntity()
->save(['name' => 'john', 'is_admin' => true, 'notes' => 'some long notes']);
self::assertSame([
['id' => 1, 'name' => 'john', 'password' => null, 'is_admin' => true, 'notes' => 'some long notes', 'main_role_id' => null],
[
'id' => 1,
'name' => 'john',
'password' => null,
'is_admin' => true,
'notes' => 'some long notes',
'main_role_id' => null,
],
], $user->export());
}

Expand Down Expand Up @@ -400,6 +407,33 @@ public function testIntrospectTableToModelSetPersistence(): void
self::assertTrue($model->issetPersistence());
self::assertSame(['id', 'a'], array_keys($model->getFields()));
}

public function testJoinFieldsAreNotCreated(): void
{
$model = new TestUserWithJoin($this->db);
$migrator = $this->createMigrator($model);
self::assertSame([
'id',
'name',
'password',
'is_admin',
'notes',
'main_role_id',
'role_id',
], array_keys($migrator->table->getColumns()));
$migrator->create();

$model = $this->createMigrator()->introspectTableToModel('user');
self::assertSame([
'id',
'name',
'password',
'is_admin',
'notes',
'main_role_id',
'role_id',
], array_keys($model->getFields()));
}
}

class TestUser extends Model
Expand Down Expand Up @@ -433,3 +467,15 @@ protected function init(): void
$this->hasMany('Users', ['model' => [TestUser::class], 'ourField' => 'id', 'theirField' => 'main_role_id']);
}
}

class TestUserWithJoin extends TestUser
{
#[\Override]
protected function init(): void
{
parent::init();

$leftJoin = $this->leftJoin('role', ['masterField' => 'role_id']);
$leftJoin->addField('role_name');
}
}

0 comments on commit 7fb3edf

Please sign in to comment.