@@ -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 *
0 commit comments