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

Add join support for tables with PK different than "id" #1077

Merged
merged 19 commits into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions src/Model/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ abstract class Join
/** @var array<int, array<string, mixed>> Data indexed by spl_object_id(entity) which is populated here as the save/insert progresses. */
private array $saveBufferByOid = [];

/**
* Custom definition if $idField for of ForeignModel
* By Default ForeignModel idField is equal to 'id'
* To be used when the joined table doesn't have a field named 'id'
*/
public ?string $foreignModelIdField = null;
abbadon1334 marked this conversation as resolved.
Show resolved Hide resolved

public function __construct(string $foreignTable)
{
$this->foreignTable = $foreignTable;
Expand All @@ -107,6 +114,7 @@ protected function createFakeForeignModel(): Model
{
$fakeModel = new Model($this->getOwner()->getPersistence(), [
'table' => $this->foreignTable,
'idField' => $this->foreignModelIdField
]);
foreach ($this->getOwner()->getFields() as $ownerField) {
if ($ownerField->hasJoin() && $ownerField->getJoin()->shortName === $this->shortName) {
Expand Down
57 changes: 57 additions & 0 deletions tests/ConditionSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,63 @@ public function testExpressionJoin(): void
static::assertSame('+123 smiths', $mm2->get('contact_phone'));
}

public function testExpressionJoinForeignCustomIdField(): void
abbadon1334 marked this conversation as resolved.
Show resolved Hide resolved
{
$this->setDb([
'user' => [
1 => ['id' => 1, 'name' => 'John', 'surname' => 'Smith', 'gender' => 'M', 'contact_id' => 1],
2 => ['id' => 2, 'name' => 'Sue', 'surname' => 'Sue', 'gender' => 'F', 'contact_id' => 2],
3 => ['id' => 3, 'name' => 'Peter', 'surname' => 'Smith', 'gender' => 'M', 'contact_id' => 1],
],
'contact' => [
1 => ['custom_id' => 1, 'contact_id' => 1, 'contact_phone' => '+123 smiths'],
2 => ['custom_id' => 2, 'contact_id' => 2, 'contact_phone' => '+321 sues'],
],
]);

$m = new Model($this->db, ['table' => 'user']);
$m->addField('name');
$m->addField('gender');
$m->addField('surname');

$m->join('contact', [
'masterField' => 'contact_id',
'foreignField' => 'contact_id',
'foreignModelIdField' => 'custom_id'
])->addField('contact_phone');

$mm2 = $m->load(1);
static::assertSame('John', $mm2->get('name'));
static::assertSame('+123 smiths', $mm2->get('contact_phone'));
$mm2 = $m->load(2);
static::assertSame('Sue', $mm2->get('name'));
static::assertSame('+321 sues', $mm2->get('contact_phone'));
$mm2 = $m->load(3);
static::assertSame('Peter', $mm2->get('name'));
static::assertSame('+123 smiths', $mm2->get('contact_phone'));

$mm = clone $m;
$mm->addCondition($mm->expr('[name] = [surname]'));
$mm2 = $mm->tryLoad(1);
static::assertNull($mm2);
$mm2 = $mm->load(2);
static::assertSame('Sue', $mm2->get('name'));
static::assertSame('+321 sues', $mm2->get('contact_phone'));
$mm2 = $mm->tryLoad(3);
static::assertNull($mm2);

$mm = clone $m;
$mm->addCondition($mm->expr('\'+123 smiths\' = [contact_phone]'));
$mm2 = $mm->load(1);
static::assertSame('John', $mm2->get('name'));
static::assertSame('+123 smiths', $mm2->get('contact_phone'));
$mm2 = $mm->tryLoad(2);
static::assertNull($mm2);
$mm2 = $mm->load(3);
static::assertSame('Peter', $mm2->get('name'));
static::assertSame('+123 smiths', $mm2->get('contact_phone'));
}

public function testArrayCondition(): void
{
$this->setDb([
Expand Down
227 changes: 227 additions & 0 deletions tests/JoinSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Atk4\Data\Exception;
use Atk4\Data\Model;
use Atk4\Data\Persistence;
use Atk4\Data\Schema\TestCase;
use Doctrine\DBAL\Platforms\MySQLPlatform;

Expand Down Expand Up @@ -762,4 +763,230 @@ public function testJoinActualFieldNamesAndPrefix(): void
],
], $this->getDb());
}

public function testJoinSavingForeignCustomIdFieldRaiseException(): void
{
$this->expectException(\Atk4\Data\Persistence\Sql\ExecuteException::class);
abbadon1334 marked this conversation as resolved.
Show resolved Hide resolved

$master_model = new \Atk4\Data\Model($this->db, [
'table' => 'user_join',
]);
$master_model->addField('name');
$master_model->addField('test_id');

$joined_model = new \Atk4\Data\Model($this->db, [
'table' => 'contact_join',
'idField' => 'not_named_id',
]);
$joined_model->addField('contact_id');
$joined_model->addField('contact_phone');

$this->createMigrator($master_model)->create();
$this->createMigrator($joined_model)->create();

$master_model->import([
[
'id' => 1,
'name' => 'John',
'test_id' => 21,
],
]);

$joined_model->import([
[
'not_named_id' => 1,
'contact_id' => 21,
'contact_phone' => '+123',
],
]);

$user = new Model($this->db, ['table' => 'user_join']);
$user->addField('name');
$j = $user->join('contact_join', [
'foreignTable' => 'contact_join',
'masterField' => 'test_id',
'foreignField' => 'contact_id',
//'foreignModelIdField' => 'not_named_id',
]);
$this->createMigrator()->createForeignKey($j);
$j->addField('contact_phone');

$user = $user->load(1);

$user->set('name', 'John');
$user->set('contact_phone', '+321');

$user->save();
}

public function testJoinSavingForeignCustomIdField(): void
{
$master_model = new \Atk4\Data\Model($this->db, [
'table' => 'user_join',
]);
$master_model->addField('name');
$master_model->addField('test_id');

$joined_model = new \Atk4\Data\Model($this->db, [
'table' => 'contact_join',
'idField' => 'not_named_id',
]);
$joined_model->addField('contact_id');
$joined_model->addField('contact_phone');

$this->createMigrator($master_model)->create();
$this->createMigrator($joined_model)->create();
mvorisek marked this conversation as resolved.
Show resolved Hide resolved

$master_model->import([
[
'id' => 1,
'name' => 'John',
'test_id' => 21,
],
]);

$joined_model->import([
[
'not_named_id' => 1,
'contact_id' => 21,
'contact_phone' => '+123',
],
]);

$user = new Model($this->db, ['table' => 'user_join']);
$user->addField('name');
$j = $user->join('contact_join', [
'masterField' => 'test_id',
'foreignField' => 'contact_id',
'foreignModelIdField' => 'not_named_id',
]);
$this->createMigrator()->createForeignKey($j);
$j->addField('contact_phone');

$user = $user->load(1);

$user->set('name', 'John');
$user->set('contact_phone', '+321');

$user->save();

static::assertSame([
'user_join' => [
1 => ['id' => 1, 'name' => 'John', 'test_id' => '21'],
],
'contact_join' => [
1 => ['not_named_id' => 1, 'contact_id' => '21', 'contact_phone' => '+321'],
],
], [
'user_join' => [
1 => $master_model->load(1)->get(),
],
'contact_join' => [
1 => $joined_model->load(1)->get(),
],
]);
}

public function testJoinDeleteForeignCustomIdField(): void
{
$master_model = new \Atk4\Data\Model($this->db, [
'table' => 'user_join',
]);
$master_model->addField('name');
$master_model->addField('test_id');

$this->createMigrator($master_model)->create();

$master_model->import([
[
'id' => 1,
'name' => 'John',
'test_id' => 21,
],
]);

$joined_model = new \Atk4\Data\Model($this->db, [
'table' => 'contact_join',
'idField' => 'not_named_id',
]);
$joined_model->addField('contact_id');
$joined_model->addField('contact_phone');

$this->createMigrator($joined_model)->create();

$joined_model->import([
[
'not_named_id' => 1,
'contact_id' => 21,
'contact_phone' => '+123',
],
]);

$user = new Model($this->db, ['table' => 'user_join']);
$user->addField('name');
$j = $user->join('contact_join', [
'masterField' => 'test_id',
'foreignField' => 'contact_id',
'foreignModelIdField' => 'not_named_id',
]);
$this->createMigrator()->createForeignKey($j);
$j->addField('contact_phone');

$user->delete(1);

static::assertSame(null, $master_model->tryLoad(1));
static::assertSame(null, $joined_model->tryLoad(1));
}

public function testJoinDeleteForeignCustomIdFieldReverse(): void
{
$master_model = new \Atk4\Data\Model($this->db, [
abbadon1334 marked this conversation as resolved.
Show resolved Hide resolved
'table' => 'user_join',
]);
$master_model->addField('name');
$master_model->addField('test_id');

$this->createMigrator($master_model)->create();

$master_model->import([
[
'id' => 1,
'name' => 'John',
'test_id' => 21,
],
]);

$joined_model = new \Atk4\Data\Model($this->db, [
'table' => 'contact_join',
'idField' => 'not_named_id',
]);
$joined_model->addField('contact_id');
$joined_model->addField('contact_phone');

$this->createMigrator($joined_model)->create();

$joined_model->import([
[
'not_named_id' => 1,
'contact_id' => 21,
'contact_phone' => '+123',
],
]);

$user = new Model($this->db, ['table' => 'user_join']);
$user->addField('name');
$j = $user->join('contact_join', [
'masterField' => 'test_id',
'foreignField' => 'contact_id',
'foreignModelIdField' => 'not_named_id',
'reverse' => true,
]);
$this->createMigrator()->createForeignKey($j);
$j->addField('contact_phone');

$user->delete(1);

static::assertSame(null, $master_model->tryLoad(1));
static::assertSame(null, $joined_model->tryLoad(1));
}
}