Repository: atk4/data (php)

Issue:
Migrator::create() adds Fields from Join to Model table
Some Code from my application (Tour Model):
```php
        $ttJoin = $this->leftJoin('tour_type', ['masterField' => 'tour_type_id']);

        $ttJoin->addField('activity_id', ['system' => true]);
```
When using `Migrator::create()` to create a table for this Model, the field `activity_id` is added.

I will provide a minimal test to reproduce. 

Pull request description:
Fix Migrator with joined/imported fields
fix #1226

Currently, migrator does create Model fields that are defined by `Model::leftJoin()` within the model table when exexuting `Migrator::create()`. 
As these fields are defined by a weak join, they should not be created within the model table but their values should be loaded from the joined table on load.

Patch:
diff --git a/src/Schema/Migrator.php b/src/Schema/Migrator.php
index dd90e37c..526c1f2a 100644
--- a/src/Schema/Migrator.php
+++ b/src/Schema/Migrator.php
@@ -290,27 +290,31 @@ class Migrator
         $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),
             ];
 


Test patch:
diff --git a/tests/Schema/MigratorTest.php b/tests/Schema/MigratorTest.php
index 12201dfb..fac8b4f4 100644
--- a/tests/Schema/MigratorTest.php
+++ b/tests/Schema/MigratorTest.php
@@ -314,7 +314,14 @@ class MigratorTest extends TestCase
         $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());
     }
 
@@ -400,6 +407,33 @@ class MigratorTest extends TestCase
         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
@@ -433,3 +467,15 @@ class TestRole extends Model
         $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');
+    }
+}