From da0d2d65a78b10f8359c517aaba65b50d22c76ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Tue, 31 May 2022 20:19:43 +0200 Subject: [PATCH 1/2] fix Join::hasMany seeding --- src/Model/Join.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Model/Join.php b/src/Model/Join.php index d6983bf6e..7867947f5 100644 --- a/src/Model/Join.php +++ b/src/Model/Join.php @@ -351,12 +351,6 @@ public function hasOne(string $link, array $defaults = []) */ public function hasMany(string $link, array $defaults = []) { - $id_field = $this->getOwner()->id_field; - $defaults = array_merge([ - 'our_field' => $id_field, - 'their_field' => $this->getModelTableString($this->getOwner()) . '_' . $id_field, - ], $defaults); - return $this->getOwner()->hasMany($link, $defaults); } From aad22d0c9292e88900a409ab8147cccd6f072811 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Thu, 2 Jun 2022 00:29:51 +0200 Subject: [PATCH 2/2] Strip schema from table for Join::foreign_field guess --- src/Model/Join.php | 4 +++- tests/JoinArrayTest.php | 19 +++++++++++-------- tests/ReferenceTest.php | 6 +++--- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/Model/Join.php b/src/Model/Join.php index 7867947f5..f965a455a 100644 --- a/src/Model/Join.php +++ b/src/Model/Join.php @@ -103,6 +103,8 @@ public function __construct(string $foreignTable = null) $this->foreign_table = $foreignTable; // handle foreign table containing a dot - that will be reverse join + // TODO this table split condition makes JoinArrayTest::testForeignFieldNameGuessTableWithSchema test + // quite unconsistent - drop it? if (str_contains($this->foreign_table, '.')) { // split by LAST dot in foreign_table name [$this->foreign_table, $this->foreign_field] = preg_split('~\.+(?=[^.]+$)~', $this->foreign_table); @@ -238,7 +240,7 @@ protected function init(): void } if (!$this->foreign_field) { - $this->foreign_field = $this->getModelTableString($this->getOwner()) . '_' . $id_field; + $this->foreign_field = preg_replace('~^.+?\.~', '', $this->getModelTableString($this->getOwner())) . '_' . $id_field; } } else { $this->reverse = false; diff --git a/tests/JoinArrayTest.php b/tests/JoinArrayTest.php index b39951a6d..da561c332 100644 --- a/tests/JoinArrayTest.php +++ b/tests/JoinArrayTest.php @@ -385,16 +385,19 @@ public function testLoadMissing(): void $m_u = $m_u->load(2); } - /* - public function testTrickyCases(): void + public function testForeignFieldNameGuessTableWithSchema(): void { $db = new Persistence\Array_(); - $m = new Model($db); - // tricky cases to testt - // - //$m->join('foo.bar', ['master_field' => 'baz']); - // foreign_table = 'foo.bar' + $m = new Model($db, ['table' => 'db.user']); + $j = $m->join('contact'); + $this->assertFalse($this->getProtected($j, 'reverse')); + $this->assertSame('contact_id', $this->getProtected($j, 'master_field')); + $this->assertSame('id', $this->getProtected($j, 'foreign_field')); + + $j = $m->join('contact2', ['reverse' => true]); + $this->assertTrue($this->getProtected($j, 'reverse')); + $this->assertSame('id', $this->getProtected($j, 'master_field')); + $this->assertSame('user_id', $this->getProtected($j, 'foreign_field')); } - */ } diff --git a/tests/ReferenceTest.php b/tests/ReferenceTest.php index f554c4bc8..34baedd6c 100644 --- a/tests/ReferenceTest.php +++ b/tests/ReferenceTest.php @@ -116,10 +116,10 @@ public function testCustomRef(): void $this->assertSame('user_archive', $m->ref('archive')->table); } - public function testOtherDbFieldRef(): void + public function testTheirFieldNameGuessTableWithSchema(): void { - $user = new Model($this->db, ['table' => 'thisdb.user']); - $order = new Model($this->db, ['table' => 'otherdb.orders']); + $user = new Model($this->db, ['table' => 'db1.user']); + $order = new Model($this->db, ['table' => 'db2.orders']); $order->addField('user_id'); $user->hasMany('Orders', ['model' => $order, 'caption' => 'My Orders']);