diff --git a/src/Model/Join.php b/src/Model/Join.php index d6983bf6e..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; @@ -351,12 +353,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); } 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']);