Skip to content

Commit 1b24f4e

Browse files
authored
Remove ActiveRecord::updateProperties() (#419)
1 parent 81ff95c commit 1b24f4e

File tree

3 files changed

+55
-62
lines changed

3 files changed

+55
-62
lines changed

src/AbstractActiveRecord.php

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -675,9 +675,9 @@ public function assignOldValues(array|null $propertyValues = null): void
675675
$this->oldValues = $propertyValues;
676676
}
677677

678-
public function update(array|null $propertyNames = null): int
678+
public function update(array|null $properties = null): int
679679
{
680-
return $this->updateInternal($propertyNames);
680+
return $this->updateInternal($properties);
681681
}
682682

683683
public function updateAll(array $propertyValues, array|string $condition = [], array $params = []): int
@@ -689,32 +689,6 @@ public function updateAll(array $propertyValues, array|string $condition = [], a
689689
return $command->execute();
690690
}
691691

692-
public function updateProperties(array $properties): int
693-
{
694-
$names = [];
695-
696-
foreach ($properties as $name => $value) {
697-
if (is_int($name)) {
698-
$names[] = $value;
699-
} else {
700-
$this->set($name, $value);
701-
$names[] = $name;
702-
}
703-
}
704-
705-
$values = $this->newValues($names);
706-
707-
if (empty($values) || $this->getIsNewRecord()) {
708-
return 0;
709-
}
710-
711-
$rows = $this->updateAll($values, $this->getOldPrimaryKey(true));
712-
713-
$this->oldValues = array_merge($this->oldValues ?? [], $values);
714-
715-
return $rows;
716-
}
717-
718692
/**
719693
* Updates the whole table using the provided counters and condition.
720694
*
@@ -1121,17 +1095,36 @@ protected function refreshInternal(array|ActiveRecordInterface|null $record = nu
11211095
/**
11221096
* {@see update()}
11231097
*
1124-
* @param array|null $propertyNames Property names to update.
1098+
* @param array|null $properties Property names or name-values pairs to update, `null` means all properties.
11251099
*
11261100
* @throws Exception
11271101
* @throws NotSupportedException
11281102
* @throws StaleObjectException
11291103
*
11301104
* @return int The number of rows affected.
11311105
*/
1132-
protected function updateInternal(array|null $propertyNames = null): int
1106+
protected function updateInternal(array|null $properties = null): int
11331107
{
1134-
$values = $this->newValues($propertyNames);
1108+
if ($this->getIsNewRecord()) {
1109+
throw new InvalidCallException('The record is new and cannot be updated.');
1110+
}
1111+
1112+
if ($properties === null) {
1113+
$names = $this->propertyNames();
1114+
} else {
1115+
$names = [];
1116+
1117+
foreach ($properties as $name => $value) {
1118+
if (is_int($name)) {
1119+
$names[] = $value;
1120+
} else {
1121+
$this->set($name, $value);
1122+
$names[] = $name;
1123+
}
1124+
}
1125+
}
1126+
1127+
$values = $this->newValues($names);
11351128

11361129
if (empty($values)) {
11371130
return 0;

src/ActiveRecordInterface.php

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Yiisoft\Db\Exception\Exception;
1111
use Yiisoft\Db\Exception\InvalidArgumentException;
1212
use Yiisoft\Db\Exception\InvalidConfigException;
13-
use Yiisoft\Db\Exception\NotSupportedException;
1413

1514
interface ActiveRecordInterface
1615
{
@@ -390,6 +389,10 @@ public function set(string $propertyName, mixed $value): void;
390389

391390
/**
392391
* Saves the changes to this active record into the associated database table.
392+
* You may specify the properties to be updated as list of name or name-value pairs.
393+
* If name-value pair specified, the corresponding property values will be modified.
394+
*
395+
* The method will then save the specified properties into a database.
393396
*
394397
* Only the {@see newValues() changed property values} will be saved into a database.
395398
*
@@ -402,6 +405,13 @@ public function set(string $propertyName, mixed $value): void;
402405
* $customer->update();
403406
* ```
404407
*
408+
* To update a customer record with specific properties:
409+
*
410+
* ```php
411+
* $customer = new Customer();
412+
* $customer->update(['name' => $name, 'email' => $email]);
413+
* ```
414+
*
405415
* Note that it's possible the update doesn't affect any row in the table.
406416
* In this case, this method will return 0.
407417
* For this reason, you should use the following code to check if update() is successful or not:
@@ -414,16 +424,16 @@ public function set(string $propertyName, mixed $value): void;
414424
* }
415425
* ```
416426
*
417-
* @param array|null $propertyNames List of property names that need to be saved. Defaults to `null`, meaning all
418-
* changed property values will be saved.
427+
* @param array|null $properties List of property names or name-values pairs that need to be saved.
428+
* Defaults to `null`, meaning all changed property values will be saved.
419429
*
420430
* @throws OptimisticLockException If the instance implements {@see OptimisticLockInterface} and the data being
421431
* updated is outdated.
422432
* @throws Throwable In case update failed.
423433
*
424434
* @return int The number of rows affected.
425435
*/
426-
public function update(array|null $propertyNames = null): int;
436+
public function update(array|null $properties = null): int;
427437

428438
/**
429439
* Updates the whole table using the provided property values and conditions.
@@ -461,27 +471,6 @@ public function update(array|null $propertyNames = null): int;
461471
*/
462472
public function updateAll(array $propertyValues, array|string $condition = [], array $params = []): int;
463473

464-
/**
465-
* Updates the specified properties.
466-
*
467-
* This method is a shortcut to {@see update()} when only a small set of properties need to be updated.
468-
*
469-
* You may specify the properties to be updated as name list or name-value pairs.
470-
* If the latter, the corresponding property values will be modified so.
471-
*
472-
* The method will then save the specified properties into a database.
473-
*
474-
* Note that this method will **not** perform data validation and will **not** trigger events.
475-
*
476-
* @param array $properties The properties (names or name-value pairs) to be updated.
477-
*
478-
* @throws Exception
479-
* @throws NotSupportedException
480-
*
481-
* @return int The number of rows affected.
482-
*/
483-
public function updateProperties(array $properties): int;
484-
485474
/**
486475
* Destroys the relationship between two records.
487476
*

tests/ActiveQueryTest.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1919,20 +1919,31 @@ public function testUpdateProperties(): void
19191919
$orderQuery = new ActiveQuery(Order::class);
19201920
$order = $orderQuery->findByPk(1);
19211921
$newTotal = 978;
1922-
$this->assertSame(1, $order->updateProperties(['total' => $newTotal]));
1922+
$this->assertSame(1, $order->update(['total' => $newTotal]));
19231923
$this->assertEquals($newTotal, $order->getTotal());
19241924

1925-
$order = $orderQuery->findByPk(1);
1925+
$order->refresh();
19261926
$this->assertEquals($newTotal, $order->getTotal());
19271927

1928+
// update only one property
1929+
$this->assertSame(1, $order->getCustomerId());
1930+
1931+
$order->set('total', 1000);
1932+
$order->set('customer_id', 2);
1933+
1934+
$this->assertSame(1, $order->update(['total']));
1935+
$order->refresh();
1936+
$this->assertEquals(1000, $order->getTotal());
1937+
$this->assertSame(1, $order->getCustomerId());
1938+
19281939
/** @see https://github.com/yiisoft/yii2/issues/12143 */
19291940
$newOrder = new Order();
19301941
$this->assertTrue($newOrder->getIsNewRecord());
19311942

1932-
$newTotal = 200;
1933-
$this->assertSame(0, $newOrder->updateProperties(['total' => $newTotal]));
1934-
$this->assertTrue($newOrder->getIsNewRecord());
1935-
$this->assertEquals($newTotal, $newOrder->getTotal());
1943+
$this->expectException(InvalidCallException::class);
1944+
$this->expectExceptionMessage('The record is new and cannot be updated.');
1945+
1946+
$this->assertSame(0, $newOrder->update(['total' => 200]));
19361947
}
19371948

19381949
/**

0 commit comments

Comments
 (0)