Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip schema from table for Join::foreign_field guess #1003

Merged
merged 2 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions 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;
mvorisek marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
$this->reverse = false;
Expand Down Expand Up @@ -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);
}

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