Skip to content

Commit

Permalink
Strip schema from table for Join::foreign_field guess
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jun 1, 2022
1 parent da0d2d6 commit aad22d0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/Model/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
19 changes: 11 additions & 8 deletions tests/JoinArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
*/
}
6 changes: 3 additions & 3 deletions tests/ReferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down

0 comments on commit aad22d0

Please sign in to comment.