Skip to content

Commit ca6d7b8

Browse files
committed
Join on primary key of join table, fall back to primary key of main table
1 parent 9d51825 commit ca6d7b8

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

Tests/Unit/JoinTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
class JoinTest extends \PHPUnit\Framework\TestCase
6+
{
7+
public function testBadTableJoin() : void
8+
{
9+
$this->expectException(\PHPFUI\ORM\Exception::class);
10+
$table = new \Tests\App\Table\InventoryTransaction();
11+
$table->addJoin('fred');
12+
}
13+
14+
public function testBadJoinType() : void
15+
{
16+
$this->expectException(\PHPFUI\ORM\Exception::class);
17+
$table = new \Tests\App\Table\InventoryTransaction();
18+
$table->addJoin('inventory_transaction_type');
19+
$table->addJoin('fred', type:'fred');
20+
}
21+
22+
public function testBadJoin() : void
23+
{
24+
$this->expectException(\PHPFUI\ORM\Exception::class);
25+
$table = new \Tests\App\Table\InventoryTransaction();
26+
$table->addJoin('employee');
27+
}
28+
}
29+

src/PHPFUI/ORM/Table.php

+24-2
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,13 @@ public function addGroupBy(string $field, bool $rollup = false) : static
134134
* Add a join with another table
135135
*
136136
* @param string $table name of the table to join, case sensitive
137-
* @param string | \PHPFUI\ORM\Condition $on condition. If string, name of field on the $table. Defaults to table name appended with Id. Or \PHPFUI\ORM\Condition for complex joins
138-
* @param string $type of join
137+
* @param string | \PHPFUI\ORM\Condition $on condition.
138+
* - If $on is empty, then the following defaults are tried:
139+
* * Join on the primary key of the join table if it exists on both tables
140+
* * If field does not exist on both tables, then use the primary key of the main table
141+
* - If $on is a non-empty string, use as the join field
142+
* - Use \PHPFUI\ORM\Condition for complex joins
143+
* @param string $type of join (LEFT, INNER, OUTER, RIGHT, FULL, CROSS)
139144
*/
140145
public function addJoin(string $table, string | \PHPFUI\ORM\Condition $on = '', string $type = 'LEFT', string $as = '') : static
141146
{
@@ -168,6 +173,23 @@ public function addJoin(string $table, string | \PHPFUI\ORM\Condition $on = '',
168173
{
169174
$on = $joinField;
170175
}
176+
$thisFields = $this->getFields();
177+
$joinFields = $joinTable->getFields();
178+
$keys = $this->getPrimaryKeys();
179+
$thisPrimaryKey = array_shift($keys);
180+
if (array_key_exists($on, $thisFields) && array_key_exists($on, $joinFields))
181+
{
182+
// do nothing here, just exclude this case
183+
}
184+
else if (array_key_exists($thisPrimaryKey, $joinFields))
185+
{
186+
// join on master table primary key
187+
$on = $thisPrimaryKey;
188+
}
189+
else
190+
{
191+
throw new \PHPFUI\ORM\Exception("Table {$table} does not have a field to join on (tried {$on} and {$thisPrimaryKey})");
192+
}
171193
$onCondition = new \PHPFUI\ORM\Condition(new \PHPFUI\ORM\Field($table . '.' . $on), new \PHPFUI\ORM\Field($this->getTableName() . '.' . $on));
172194
}
173195
else

0 commit comments

Comments
 (0)