-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix reverse join inconsistencies and defaults (#802)
* fix issues in join * fix join defaults * finalize tests * add test with aliases * Update src/Model/Join.php Co-authored-by: Michael Voříšek <[email protected]> * Update src/Model/Join.php Co-authored-by: Michael Voříšek <[email protected]> * Update tests/JoinArrayTest.php Co-authored-by: Michael Voříšek <[email protected]> * Update JoinSqlTest.php * Update tests/JoinArrayTest.php * Update tests/JoinSqlTest.php * Update src/Model/Join.php Co-authored-by: Michael Voříšek <[email protected]>
- Loading branch information
1 parent
9f888c0
commit b9a1ceb
Showing
3 changed files
with
97 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ public function testDirection() | |
$this->assertSame('test_id', $this->getProtected($j, 'master_field')); | ||
$this->assertSame('id', $this->getProtected($j, 'foreign_field')); | ||
|
||
$this->expectException(Exception::class); // TODO not implemented yet, see https://github.com/atk4/data/issues/803 | ||
$j = $m->join('contact4.foo_id', ['test_id', 'reverse' => true]); | ||
$this->assertTrue($this->getProtected($j, 'reverse')); | ||
$this->assertSame('test_id', $this->getProtected($j, 'master_field')); | ||
|
@@ -599,4 +600,76 @@ public function testJoinHasOneHasMany() | |
['id' => 41, 'contact_id' => 10, 'address' => '[email protected]'], | ||
], $m_u->ref('Email')->export()); | ||
} | ||
|
||
public function testJoinReverseOneOnOne() | ||
{ | ||
$this->setDb([ | ||
'user' => [ | ||
10 => ['id' => 10, 'name' => 'John'], | ||
20 => ['id' => 20, 'name' => 'Peter'], | ||
], 'detail' => [ | ||
100 => ['id' => 100, 'my_user_id' => 10, 'notes' => 'first note'], | ||
200 => ['id' => 200, 'my_user_id' => 20, 'notes' => 'second note'], | ||
], | ||
]); | ||
|
||
$db = new Persistence\Sql($this->db->connection); | ||
$m_user = new Model($db, 'user'); | ||
$m_user->addField('name'); | ||
$j = $m_user->join('detail.my_user_id', [ | ||
//'reverse' => true, // this will be reverse join by default | ||
// also no need to set these (will be done automatically), but still let's do that for test sake | ||
'master_field' => 'id', | ||
'foreign_field' => 'my_user_id', | ||
]); | ||
$j->addField('notes'); | ||
|
||
// try load one record | ||
$m = (clone $m_user)->tryLoad(20); | ||
$this->assertTrue($m->loaded()); | ||
$this->assertEquals(['id' => 20, 'name' => 'Peter', 'notes' => 'second note'], $m->get()); | ||
|
||
// try to update loaded record | ||
$m->save(['name' => 'Mark', 'notes' => '2nd note']); | ||
$m = (clone $m_user)->tryLoad(20); | ||
$this->assertTrue($m->loaded()); | ||
$this->assertEquals(['id' => 20, 'name' => 'Mark', 'notes' => '2nd note'], $m->get()); | ||
|
||
// insert new record | ||
$m = (clone $m_user)->save(['name' => 'Emily', 'notes' => '3rd note']); | ||
$m = (clone $m_user)->tryLoad(21); | ||
$this->assertTrue($m->loaded()); | ||
$this->assertEquals(['id' => 21, 'name' => 'Emily', 'notes' => '3rd note'], $m->get()); | ||
|
||
// now test reverse join defined differently | ||
$m_user = new Model($db, 'user'); | ||
$m_user->addField('name'); | ||
$j = $m_user->join('detail', [ // here we just set foreign table name without dot and foreign_field | ||
'reverse' => true, // and set it as revers join | ||
'foreign_field' => 'my_user_id', // this is custome name so we have to set it here otherwise it will generate user_id | ||
]); | ||
$j->addField('notes'); | ||
|
||
// insert new record | ||
$m = (clone $m_user)->save(['name' => 'Olaf', 'notes' => '4th note']); | ||
$m = (clone $m_user)->tryLoad(22); | ||
$this->assertTrue($m->loaded()); | ||
$this->assertEquals(['id' => 22, 'name' => 'Olaf', 'notes' => '4th note'], $m->get()); | ||
|
||
// now test reverse join with table_alias and foreign_alias | ||
$m_user = new Model($db, ['user', 'table_alias' => 'u']); | ||
$m_user->addField('name'); | ||
$j = $m_user->join('detail', [ | ||
'reverse' => true, | ||
'foreign_field' => 'my_user_id', | ||
'foreign_alias' => 'a', | ||
]); | ||
$j->addField('notes'); | ||
|
||
// insert new record | ||
$m = (clone $m_user)->save(['name' => 'Chris', 'notes' => '5th note']); | ||
$m = (clone $m_user)->tryLoad(23); | ||
$this->assertTrue($m->loaded()); | ||
$this->assertEquals(['id' => 23, 'name' => 'Chris', 'notes' => '5th note'], $m->get()); | ||
} | ||
} |