Skip to content

Commit 05c3daa

Browse files
authored
Merge commit from fork
1 parent 8aa8ea0 commit 05c3daa

6 files changed

Lines changed: 415 additions & 8 deletions

File tree

‎system/BaseModel.php‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ abstract class BaseModel
129129

130130
/**
131131
* Determines whether the model should protect field names during
132-
* mass assignment operations such as $this->insert(), $this->update().
132+
* mass assignment operations such as $this->insert(), $this->update(), $this->replace().
133133
*
134134
* When set to `true`, only the fields explicitly defined in the `$allowedFields`
135135
* property will be allowed for mass assignment. This helps prevent
@@ -142,7 +142,7 @@ abstract class BaseModel
142142

143143
/**
144144
* An array of field names that are allowed
145-
* to be set by the user in inserts/updates.
145+
* to be set by the user in inserts/updates/replaces.
146146
*
147147
* @var list<string>
148148
*/
@@ -1265,6 +1265,8 @@ public function onlyDeleted()
12651265
* @param bool $returnSQL `true` means SQL is returned, `false` will execute the query.
12661266
*
12671267
* @return BaseResult|false|Query|string
1268+
*
1269+
* @throws DataException
12681270
*/
12691271
public function replace(?array $row = null, bool $returnSQL = false)
12701272
{
@@ -1273,7 +1275,14 @@ public function replace(?array $row = null, bool $returnSQL = false)
12731275
return false;
12741276
}
12751277

1276-
$row = (array) $row;
1278+
$row = (array) $row;
1279+
$hasData = $row !== [];
1280+
$row = $this->doProtectFieldsForReplace($row);
1281+
1282+
if ($hasData && $row === []) {
1283+
throw DataException::forEmptyDataset('replace');
1284+
}
1285+
12771286
$row = $this->setCreatedField($row, $this->setDate());
12781287
$row = $this->setUpdatedField($row, $this->setDate());
12791288

@@ -1414,6 +1423,20 @@ protected function doProtectFieldsForInsert(array $row): array
14141423
return $this->doProtectFields($row);
14151424
}
14161425

1426+
/**
1427+
* Protects replacement data before generated timestamps are added.
1428+
*
1429+
* @param row_array $row
1430+
*
1431+
* @return row_array
1432+
*
1433+
* @throws DataException
1434+
*/
1435+
protected function doProtectFieldsForReplace(array $row): array
1436+
{
1437+
return $this->doProtectFields($row);
1438+
}
1439+
14171440
/**
14181441
* Sets the timestamp or current timestamp if null value is passed.
14191442
*

‎system/Model.php‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,22 @@ protected function doProtectFieldsForInsert(array $row): array
695695
return $row;
696696
}
697697

698+
protected function doProtectFieldsForReplace(array $row): array
699+
{
700+
$primaryKey = array_intersect_key($row, [$this->primaryKey => true]);
701+
$protectedRow = $this->doProtectFields($row);
702+
703+
// Reject data reduced to an identifier, but preserve explicit identifier-only calls.
704+
if ($row !== $primaryKey && array_diff_key($protectedRow, $primaryKey) === []) {
705+
throw DataException::forEmptyDataset('replace');
706+
}
707+
708+
$protectedRow += $primaryKey;
709+
710+
// PostgreSQL's replace() uses the first field to match an existing row.
711+
return array_replace(array_intersect_key($row, $protectedRow), $protectedRow);
712+
}
713+
698714
public function update($id = null, $row = null): bool
699715
{
700716
if (isset($this->tempData['data'])) {

0 commit comments

Comments
 (0)