Skip to content

Commit 9026fd6

Browse files
authored
feat: add insert-only fields to Model (#10350)
* feat(model): add insert-only fields - add $insertOnlyFields and setInsertOnlyFields() to Model - prevent configured fields from being submitted during update operations - allow insert-only fields during insert and insertBatch - document the Model-level protection boundaries - add coverage for update, save, updateBatch, entity, and protect(false) paths Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com> * trigger CI Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com> * refactor: align insert-only fields with field protection Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com> * test: update generated model expectations Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com> --------- Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
1 parent 7111c24 commit 9026fd6

11 files changed

Lines changed: 388 additions & 17 deletions

File tree

‎system/BaseModel.php‎

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ abstract class BaseModel
155155
*/
156156
protected $allowedFields = [];
157157

158+
/**
159+
* Fields that may be inserted but not updated through Model write methods.
160+
*
161+
* @var list<string>
162+
*/
163+
protected array $insertOnlyFields = [];
164+
158165
/**
159166
* If true, will set created_at, and updated_at
160167
* values during insert and update routines.
@@ -1162,7 +1169,10 @@ public function updateBatch(?array $set = null, ?string $index = null, int $batc
11621169

11631170
// Must be called first so we don't
11641171
// strip out updated_at values.
1165-
$this->ensureNoDisallowedFields($row, $index === null ? [] : [$index]);
1172+
$ignoredFields = $index === null ? [] : [$index];
1173+
1174+
$row = $this->doProtectInsertOnlyFieldsForUpdate($row, $ignoredFields);
1175+
$this->ensureNoDisallowedFields($row, $ignoredFields);
11661176
$row = $this->doProtectFields($row);
11671177

11681178
// Restore updateIndex value in case it was wiped out
@@ -1375,6 +1385,20 @@ public function setAllowedFields(array $allowedFields)
13751385
return $this;
13761386
}
13771387

1388+
/**
1389+
* Sets the fields that may be inserted but not updated.
1390+
*
1391+
* @param list<string> $insertOnlyFields
1392+
*
1393+
* @return $this
1394+
*/
1395+
public function setInsertOnlyFields(array $insertOnlyFields)
1396+
{
1397+
$this->insertOnlyFields = $insertOnlyFields;
1398+
1399+
return $this;
1400+
}
1401+
13781402
/**
13791403
* Sets whether or not we should whitelist data set during
13801404
* updates or inserts against $this->availableFields.
@@ -1462,6 +1486,42 @@ protected function ensureNoDisallowedFields(array $row, array $ignoredFields = [
14621486
}
14631487
}
14641488

1489+
/**
1490+
* Removes fields from update data when they may only be inserted.
1491+
*
1492+
* @param row_array $row
1493+
* @param list<string> $ignoredFields
1494+
*
1495+
* @return row_array
1496+
*
1497+
* @throws DataException
1498+
*/
1499+
protected function doProtectInsertOnlyFieldsForUpdate(array $row, array $ignoredFields = []): array
1500+
{
1501+
if (! $this->protectFields || $this->allowedFields === [] || $this->insertOnlyFields === []) {
1502+
return $row;
1503+
}
1504+
1505+
$insertOnlyFields = [];
1506+
1507+
foreach (array_keys($row) as $key) {
1508+
if (in_array($key, $ignoredFields, true)) {
1509+
continue;
1510+
}
1511+
1512+
if (in_array($key, $this->insertOnlyFields, true)) {
1513+
$insertOnlyFields[] = $key;
1514+
unset($row[$key]);
1515+
}
1516+
}
1517+
1518+
if ($insertOnlyFields !== [] && $this->throwOnDisallowedFields) {
1519+
throw DataException::forInsertOnlyFields(static::class, $insertOnlyFields);
1520+
}
1521+
1522+
return $row;
1523+
}
1524+
14651525
/**
14661526
* Ensures that only the fields that are allowed to be inserted are in
14671527
* the data array.
@@ -1496,6 +1556,7 @@ protected function doProtectFieldsForInsert(array $row): array
14961556
*/
14971557
protected function doProtectFieldsForUpdate(array $row): array
14981558
{
1559+
$row = $this->doProtectInsertOnlyFieldsForUpdate($row);
14991560
$this->ensureNoDisallowedFields($row);
15001561

15011562
return $this->doProtectFields($row);

‎system/Commands/Generators/Views/model.tpl.php‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
class {class} extends Model
88
{
99
<?php if (is_string($dbGroup)): ?>
10-
protected $DBGroup = '{dbGroup}';
10+
protected $DBGroup = '{dbGroup}';
1111
<?php endif; ?>
12-
protected $table = '{table}';
13-
protected $primaryKey = 'id';
14-
protected $useAutoIncrement = true;
15-
protected $returnType = {return};
16-
protected $useSoftDeletes = false;
17-
protected $protectFields = true;
18-
protected $allowedFields = [];
12+
protected $table = '{table}';
13+
protected $primaryKey = 'id';
14+
protected $useAutoIncrement = true;
15+
protected $returnType = {return};
16+
protected $useSoftDeletes = false;
17+
protected $protectFields = true;
18+
protected $allowedFields = [];
19+
protected array $insertOnlyFields = [];
1920

2021
protected bool $throwOnDisallowedFields = false;
2122
protected bool $allowEmptyInserts = false;

‎system/Database/Exceptions/DataException.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ public static function forDisallowedFields(string $model, array $fields)
8383
return new static(lang('Database.disallowedFields', [$model, implode(', ', $fields)]));
8484
}
8585

86+
/**
87+
* @param list<string> $fields
88+
*
89+
* @return DataException
90+
*/
91+
public static function forInsertOnlyFields(string $model, array $fields)
92+
{
93+
return new static(lang('Database.insertOnlyFields', [$model, implode(', ', $fields)]));
94+
}
95+
8696
/**
8797
* @return DataException
8898
*/

‎system/Language/en/Database.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
'invalidArgument' => 'You must provide a valid "{0}".',
1818
'invalidAllowedFields' => 'Allowed fields must be specified for model: "{0}"',
1919
'disallowedFields' => 'Fields are not allowed for model "{0}": {1}',
20+
'insertOnlyFields' => 'Fields cannot be updated for model "{0}": {1}',
2021
'emptyDataset' => 'There is no data to {0}.',
2122
'emptyPrimaryKey' => 'There is no primary key defined when trying to make {0}.',
2223
'failGetFieldData' => 'Failed to get field data from database.',

‎system/Model.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,7 @@ protected function doProtectFieldsForInsert(array $row): array
785785

786786
protected function doProtectFieldsForUpdate(array $row): array
787787
{
788+
$row = $this->doProtectInsertOnlyFieldsForUpdate($row, [$this->primaryKey]);
788789
$this->ensureNoDisallowedFields($row, [$this->primaryKey]);
789790

790791
return $this->doProtectFields($row);

‎tests/system/Commands/Generators/ModelGeneratorTest.php‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ public function testGenerateModel(): void
5353
$file = APPPATH . 'Models/User.php';
5454
$this->assertFileExists($file);
5555
$this->assertStringContainsString('extends Model', $this->getFileContent($file));
56-
$this->assertStringContainsString('protected $table = \'users\';', $this->getFileContent($file));
57-
$this->assertStringContainsString('protected $returnType = \'array\';', $this->getFileContent($file));
56+
$this->assertStringContainsString('protected $table = \'users\';', $this->getFileContent($file));
57+
$this->assertStringContainsString('protected $returnType = \'array\';', $this->getFileContent($file));
5858
}
5959

6060
public function testGenerateModelWithOptionTable(): void
@@ -63,7 +63,7 @@ public function testGenerateModelWithOptionTable(): void
6363
$this->assertStringContainsString('File created: ', $this->getStreamFilterBuffer());
6464
$file = APPPATH . 'Models/Cars.php';
6565
$this->assertFileExists($file);
66-
$this->assertStringContainsString('protected $table = \'utilisateur\';', $this->getFileContent($file));
66+
$this->assertStringContainsString('protected $table = \'utilisateur\';', $this->getFileContent($file));
6767
}
6868

6969
public function testGenerateModelWithOptionDBGroup(): void
@@ -72,7 +72,7 @@ public function testGenerateModelWithOptionDBGroup(): void
7272
$this->assertStringContainsString('File created: ', $this->getStreamFilterBuffer());
7373
$file = APPPATH . 'Models/User.php';
7474
$this->assertFileExists($file);
75-
$this->assertStringContainsString('protected $DBGroup = \'testing\';', $this->getFileContent($file));
75+
$this->assertStringContainsString('protected $DBGroup = \'testing\';', $this->getFileContent($file));
7676
}
7777

7878
public function testGenerateModelWithOptionReturnArray(): void
@@ -81,7 +81,7 @@ public function testGenerateModelWithOptionReturnArray(): void
8181
$this->assertStringContainsString('File created: ', $this->getStreamFilterBuffer());
8282
$file = APPPATH . 'Models/User.php';
8383
$this->assertFileExists($file);
84-
$this->assertStringContainsString('protected $returnType = \'array\';', $this->getFileContent($file));
84+
$this->assertStringContainsString('protected $returnType = \'array\';', $this->getFileContent($file));
8585
}
8686

8787
public function testGenerateModelWithOptionReturnObject(): void
@@ -90,7 +90,7 @@ public function testGenerateModelWithOptionReturnObject(): void
9090
$this->assertStringContainsString('File created: ', $this->getStreamFilterBuffer());
9191
$file = APPPATH . 'Models/User.php';
9292
$this->assertFileExists($file);
93-
$this->assertStringContainsString('protected $returnType = \'object\';', $this->getFileContent($file));
93+
$this->assertStringContainsString('protected $returnType = \'object\';', $this->getFileContent($file));
9494
}
9595

9696
public function testGenerateModelWithOptionReturnEntity(): void
@@ -100,7 +100,7 @@ public function testGenerateModelWithOptionReturnEntity(): void
100100

101101
$file = APPPATH . 'Models/User.php';
102102
$this->assertFileExists($file);
103-
$this->assertStringContainsString('protected $returnType = \App\Entities\User::class;', $this->getFileContent($file));
103+
$this->assertStringContainsString('protected $returnType = \App\Entities\User::class;', $this->getFileContent($file));
104104

105105
if (is_file($file)) {
106106
unlink($file);

0 commit comments

Comments
 (0)