-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: add insert-only fields to Model #10350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
memleakd
wants to merge
4
commits into
codeigniter4:4.8
Choose a base branch
from
memleakd:feat/model-insert-only-fields
base: 4.8
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * This file is part of CodeIgniter 4 framework. | ||
| * | ||
| * (c) CodeIgniter Foundation <admin@codeigniter.com> | ||
| * | ||
| * For the full copyright and license information, please view | ||
| * the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace CodeIgniter\Models; | ||
|
|
||
| use CodeIgniter\Database\Exceptions\DataException; | ||
| use PHPUnit\Framework\Attributes\Group; | ||
| use Tests\Support\Entity\User; | ||
| use Tests\Support\Models\UserModel; | ||
| use Tests\Support\Models\ValidModel; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| #[Group('DatabaseLive')] | ||
| final class InsertOnlyFieldsModelTest extends LiveModelTestCase | ||
| { | ||
| public function testInsertAllowsInsertOnlyFields(): void | ||
| { | ||
| $this->createModel(UserModel::class)->setInsertOnlyFields(['email'])->insert([ | ||
| 'name' => 'Insert Only', | ||
| 'email' => 'insert-only@example.com', | ||
| 'country' => 'US', | ||
| ]); | ||
|
|
||
| $this->seeInDatabase('user', [ | ||
| 'email' => 'insert-only@example.com', | ||
| ]); | ||
| } | ||
|
|
||
| public function testInsertBatchAllowsInsertOnlyFields(): void | ||
| { | ||
| $result = $this->createModel(UserModel::class)->setInsertOnlyFields(['email'])->insertBatch([ | ||
| [ | ||
| 'name' => 'Insert Only Batch', | ||
| 'email' => 'insert-only-batch@example.com', | ||
| 'country' => 'US', | ||
| ], | ||
| ]); | ||
|
|
||
| $this->assertSame(1, $result); | ||
| $this->seeInDatabase('user', [ | ||
| 'email' => 'insert-only-batch@example.com', | ||
| ]); | ||
| } | ||
|
|
||
| public function testUpdateThrowsOnInsertOnlyFields(): void | ||
| { | ||
| $this->expectException(DataException::class); | ||
| $this->expectExceptionMessage('Fields cannot be updated for model "Tests\Support\Models\UserModel": email'); | ||
|
|
||
| $this->createModel(UserModel::class)->setInsertOnlyFields(['email'])->update(1, [ | ||
| 'name' => 'Insert Only Update', | ||
| 'email' => 'insert-only-update@example.com', | ||
| ]); | ||
| } | ||
|
|
||
| public function testSaveUpdateThrowsOnInsertOnlyFields(): void | ||
| { | ||
| $this->expectException(DataException::class); | ||
| $this->expectExceptionMessage('Fields cannot be updated for model "Tests\Support\Models\UserModel": email'); | ||
|
|
||
| $this->createModel(UserModel::class)->setInsertOnlyFields(['email'])->save([ | ||
| 'id' => 1, | ||
| 'name' => 'Insert Only Save', | ||
| 'email' => 'insert-only-save@example.com', | ||
| ]); | ||
| } | ||
|
|
||
| public function testSetUpdateThrowsOnInsertOnlyFields(): void | ||
| { | ||
| $this->expectException(DataException::class); | ||
| $this->expectExceptionMessage('Fields cannot be updated for model "Tests\Support\Models\UserModel": email'); | ||
|
|
||
| $this->createModel(UserModel::class)->setInsertOnlyFields(['email']) | ||
| ->where('id', 1) | ||
| ->set('email', 'insert-only-set@example.com') | ||
| ->update(null, ['name' => 'Insert Only Set']); | ||
| } | ||
|
|
||
| public function testUpdateBatchThrowsOnInsertOnlyFields(): void | ||
| { | ||
| $this->expectException(DataException::class); | ||
| $this->expectExceptionMessage('Fields cannot be updated for model "Tests\Support\Models\UserModel": email'); | ||
|
|
||
| $this->createModel(UserModel::class)->setInsertOnlyFields(['email'])->updateBatch([ | ||
| [ | ||
| 'id' => 1, | ||
| 'name' => 'Insert Only Batch', | ||
| 'email' => 'insert-only-update-batch@example.com', | ||
| ], | ||
| ], 'id'); | ||
| } | ||
|
|
||
| public function testUpdateBatchAllowsInsertOnlyFieldAsIndex(): void | ||
| { | ||
| $result = $this->createModel(UserModel::class)->setInsertOnlyFields(['email'])->updateBatch([ | ||
| [ | ||
| 'email' => 'derek@world.com', | ||
| 'name' => 'Insert Only Batch Index', | ||
| ], | ||
| ], 'email'); | ||
|
|
||
| $this->assertSame(1, $result); | ||
| $this->seeInDatabase('user', [ | ||
| 'email' => 'derek@world.com', | ||
| 'name' => 'Insert Only Batch Index', | ||
| ]); | ||
| } | ||
|
|
||
| public function testEntityUpdateThrowsOnChangedInsertOnlyFields(): void | ||
| { | ||
| $model = new class ($this->db) extends UserModel { | ||
| protected $returnType = User::class; | ||
| protected $insertOnlyFields = ['email']; | ||
| }; | ||
|
|
||
| $user = $model->find(1); | ||
| $this->assertInstanceOf(User::class, $user); | ||
|
|
||
| $user->email = 'insert-only-entity@example.com'; | ||
|
|
||
| $this->expectException(DataException::class); | ||
| $this->expectExceptionMessage('Fields cannot be updated for model "' . $model::class . '": email'); | ||
|
|
||
| $model->update($user->id, $user); | ||
| } | ||
|
|
||
| public function testEntityUpdateAllowsUnchangedInsertOnlyFields(): void | ||
| { | ||
| $model = new class ($this->db) extends UserModel { | ||
| protected $returnType = User::class; | ||
| protected $insertOnlyFields = ['email']; | ||
| }; | ||
|
|
||
| $user = $model->find(1); | ||
| $this->assertInstanceOf(User::class, $user); | ||
|
|
||
| $user->name = 'Insert Only Entity'; | ||
|
|
||
| $this->assertTrue($model->update($user->id, $user)); | ||
| $this->seeInDatabase('user', [ | ||
| 'id' => 1, | ||
| 'name' => 'Insert Only Entity', | ||
| ]); | ||
| } | ||
|
|
||
| public function testProtectFalseBypassesInsertOnlyFields(): void | ||
| { | ||
| $result = $this->createModel(UserModel::class)->setInsertOnlyFields(['email'])->protect(false)->update(1, [ | ||
| 'email' => 'insert-only-disabled@example.com', | ||
| ]); | ||
|
|
||
| $this->assertTrue($result); | ||
| $this->seeInDatabase('user', [ | ||
| 'id' => 1, | ||
| 'email' => 'insert-only-disabled@example.com', | ||
| ]); | ||
| } | ||
|
|
||
| public function testValidationRunsBeforeInsertOnlyFields(): void | ||
| { | ||
| $model = $this->createModel(ValidModel::class)->setInsertOnlyFields(['description']); | ||
| $this->setPrivateProperty($model, 'cleanValidationRules', false); | ||
|
|
||
| $this->assertFalse($model->update(1, [ | ||
| 'description' => 'Insert only description', | ||
| ])); | ||
| $this->assertArrayHasKey('name', $model->errors()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <?php | ||
|
|
||
| namespace App\Models; | ||
|
|
||
| use CodeIgniter\Model; | ||
|
|
||
| class UserModel extends Model | ||
| { | ||
| protected $allowedFields = ['public_id', 'name', 'email']; | ||
|
|
||
| protected $insertOnlyFields = ['public_id']; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.