Skip to content

Commit 9d51825

Browse files
committed
Support joins for table updates
1 parent 18408e8 commit 9d51825

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

Tests/Unit/UpdateTest.php

+30
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,36 @@ public function testRecordUpdate() : void
2929
$this->assertEquals(1, $customerTable->count());
3030
$this->assertEquals(0, $customerTable2->count());
3131

32+
$this->assertTrue($transaction->rollBack());
33+
$this->assertEquals(0, $customerTable->count());
34+
$this->assertEquals(1, $customerTable2->count());
35+
$customerTable->setWhere();
36+
$this->assertEquals(29, $customerTable->count());
37+
}
38+
39+
public function testTableUpdate() : void
40+
{
41+
$customerTable = new \Tests\App\Table\Customer();
42+
$this->assertEquals(29, $customerTable->count());
43+
$condition = new \PHPFUI\ORM\Condition('last_name', 'Wells');
44+
$condition->or(new \PHPFUI\ORM\Condition('first_name', 'Bruce'));
45+
$customerTable->setWhere($condition);
46+
$this->assertEquals(0, $customerTable->count());
47+
48+
$customerTable2 = new \Tests\App\Table\Customer();
49+
$condition2 = new \PHPFUI\ORM\Condition('last_name', 'Kupkova');
50+
$condition2->and(new \PHPFUI\ORM\Condition('first_name', 'Helena'));
51+
$customerTable2->setWhere($condition2);
52+
$this->assertEquals(1, $customerTable2->count());
53+
54+
$transaction = new \PHPFUI\ORM\Transaction();
55+
$customerTable2->update(['last_name' => 'Wells', 'first_name' => 'Bruce']);
56+
$customer = new \Tests\App\Record\Customer(15);
57+
$this->assertEquals('Bruce', $customer->first_name);
58+
$this->assertEquals('Wells', $customer->last_name);
59+
$this->assertEquals(1, $customerTable->count());
60+
$this->assertEquals(0, $customerTable2->count());
61+
3262
$this->assertTrue($transaction->rollBack());
3363
$this->assertEquals(0, $customerTable->count());
3464
$this->assertEquals(1, $customerTable2->count());

src/PHPFUI/ORM/Table.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -979,9 +979,13 @@ public function translate(string $field = '') : string
979979
*/
980980
public function update(array $variables) : static
981981
{
982-
$this->lastSql = 'UPDATE ' . $this->instance->getTableName() . ' SET';
983-
$comma = '';
982+
$this->lastSql = 'UPDATE ' . $this->instance->getTableName();
983+
984984
$this->lastInput = [];
985+
$this->lastSql .= ' ' . $this->getJoins($this->lastInput);
986+
987+
$this->lastSql .= ' SET';
988+
$comma = '';
985989

986990
foreach ($variables as $field => $value)
987991
{
@@ -994,7 +998,7 @@ public function update(array $variables) : static
994998
$orderBy = $this->getOrderBy();
995999
$limit = $this->getLimitClause();
9961000

997-
$this->lastSql .= $where . $orderBy . $limit;
1001+
$this->lastSql .= "{$where} {$orderBy} {$limit}";
9981002
\PHPFUI\ORM::execute($this->lastSql, $this->lastInput);
9991003

10001004
return $this;

0 commit comments

Comments
 (0)