Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -711,11 +711,6 @@ public function update($id = null, $row = null): bool
return parent::update($id, $row);
}

protected function objectToRawArray($object, bool $onlyChanged = true, bool $recursive = false): array
{
return parent::objectToRawArray($object, $onlyChanged);
}

/**
* Provides/instantiates the builder/db connection and model's table/primary key names and return type.
*
Expand Down
129 changes: 129 additions & 0 deletions tests/system/Models/ObjectToRawArrayModelTest.php
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
Comment thread
michalsn marked this conversation as resolved.
Outdated

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);
}
}
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.7.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Bugs Fixed

- **Database:** Fixed a bug where ``updateBatch()`` could be called after Query Builder ``where()`` conditions, even though it's not supported. In this situation, now the ``DatabaseException`` is thrown.
- **HTTP:** Fixed a bug where the User Agent library reported Safari's WebKit version instead of the browser version from the ``Version`` token.
- **Model:** Fixed a bug in ``Model::objectToRawArray()`` where the ``$recursive`` parameter was ignored.

See the repo's
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
Expand Down
Loading