-
Notifications
You must be signed in to change notification settings - Fork 2k
fix(model): pass $recursive parameter to parent in objectToRawArray #10258
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
Merged
paulbalandan
merged 12 commits into
codeigniter4:develop
from
gr8man:fix/model-recursive
Jun 11, 2026
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
24ad60e
fix: pass $recursive parameter to parent in Model::objectToRawArray
gr8man 26bcef9
Update tests/system/Models/ObjectToRawArrayModelTest.php
gr8man f63d78f
fix: remove redundant objectToRawArray method and update changelog
gr8man 73c8458
Update tests/system/Models/ObjectToRawArrayModelTest.php
gr8man 4a341be
Update tests/system/Models/ObjectToRawArrayModelTest.php
gr8man 3676271
Update test comment: indicate fix for model‑recursive PR #10258
gr8man 5903c24
test: Fix PHPStan missing type in callObjectToRawArray
gr8man 81ef6e6
Merge branch 'develop' into fix/model-recursive
gr8man df249d4
style: remove redundant comment in ObjectToRawArrayModelTest.php
gr8man 674c028
Merge branch 'fix/model-recursive' of https://github.com/gr8man/CodeI…
gr8man 608ef6d
Merge branch 'develop' into fix/model-recursive
gr8man a9eea30
Merge branch 'develop' into fix/model-recursive
gr8man 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| <?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; // Fixed model-recursive behavior | ||
|
|
||
| use CodeIgniter\Entity\Entity; | ||
| use CodeIgniter\Model; | ||
| use CodeIgniter\Test\CIUnitTestCase; | ||
| use PHPUnit\Framework\Attributes\Group; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| #[Group('Others')] | ||
| final class ObjectToRawArrayModelTest extends CIUnitTestCase | ||
| { | ||
| private function createModel(): Model | ||
| { | ||
| return new class () extends Model { | ||
| public function __construct() | ||
| { | ||
| // Skip DB connection — we only test objectToRawArray | ||
| } | ||
|
|
||
| protected $table = 'test'; | ||
| protected $allowedFields = ['name', 'nested', 'entity']; | ||
| protected $returnType = 'array'; | ||
| protected $useSoftDeletes = false; | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Call protected objectToRawArray via reflection. | ||
| * | ||
| * @return array<string, mixed> | ||
| */ | ||
| private function callObjectToRawArray(Model $model, object $object, bool $onlyChanged, bool $recursive): array | ||
| { | ||
| $method = self::getPrivateMethodInvoker($model, 'objectToRawArray'); | ||
|
|
||
| return $method($object, $onlyChanged, $recursive); | ||
| } | ||
|
|
||
| public function testObjectToRawArrayPassesRecursiveTrue(): void | ||
| { | ||
| $model = $this->createModel(); | ||
|
|
||
| $inner = new class () extends Entity { | ||
| protected $attributes = ['name' => 'inner']; | ||
| protected $original = ['name' => 'inner']; | ||
| }; | ||
|
|
||
| $outer = new class () extends Entity { | ||
| protected $attributes = ['name' => 'outer', 'nested' => null]; | ||
| protected $original = ['name' => 'outer', 'nested' => null]; | ||
| }; | ||
| $outer->nested = $inner; | ||
|
|
||
| $result = $this->callObjectToRawArray($model, $outer, false, true); | ||
|
|
||
| $this->assertArrayHasKey('name', $result); | ||
| $this->assertSame('outer', $result['name']); | ||
| $this->assertArrayHasKey('nested', $result); | ||
| $this->assertIsArray($result['nested']); | ||
| $this->assertSame(['name' => 'inner'], $result['nested']); | ||
| } | ||
|
|
||
| public function testObjectToRawArrayPassesRecursiveFalse(): void | ||
| { | ||
| $model = $this->createModel(); | ||
|
|
||
| $inner = new class () extends Entity { | ||
| protected $attributes = ['name' => 'inner']; | ||
| protected $original = ['name' => 'inner']; | ||
| }; | ||
|
|
||
| $outer = new class () extends Entity { | ||
| protected $attributes = ['name' => 'outer', 'nested' => null]; | ||
| protected $original = ['name' => 'outer', 'nested' => null]; | ||
| }; | ||
| $outer->nested = $inner; | ||
|
|
||
| $result = $this->callObjectToRawArray($model, $outer, false, false); | ||
|
|
||
| $this->assertArrayHasKey('name', $result); | ||
| $this->assertSame('outer', $result['name']); | ||
| $this->assertArrayHasKey('nested', $result); | ||
| // With recursive=false, nested Entity should remain as object | ||
| $this->assertInstanceOf(Entity::class, $result['nested']); | ||
| } | ||
|
|
||
| public function testObjectToRawArrayNonEntity(): void | ||
| { | ||
| $model = $this->createModel(); | ||
|
|
||
| $obj = new class () { | ||
| public string $name = 'test'; | ||
| public string $value = '123'; | ||
| }; | ||
|
|
||
| $result = $this->callObjectToRawArray($model, $obj, false, false); | ||
|
|
||
| $this->assertSame(['name' => 'test', 'value' => '123'], $result); | ||
| } | ||
|
|
||
| public function testObjectToRawArrayOnlyChanged(): void | ||
| { | ||
| $model = $this->createModel(); | ||
| $entity = new class () extends Entity { | ||
| protected $attributes = ['name' => 'original', 'value' => 'keep']; | ||
| protected $original = ['name' => 'original', 'value' => 'keep']; | ||
| }; | ||
| $entity->name = 'modified'; | ||
|
|
||
| $result = $this->callObjectToRawArray($model, $entity, true, false); | ||
|
|
||
| $this->assertSame(['name' => 'modified'], $result); | ||
| } | ||
| } | ||
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
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.