CodeIgniter4
CodeIgniter4 copied to clipboard
Bug: The model fails to save entities when they have internal entities.
PHP Version
8.0
CodeIgniter4 Version
4.2.1
CodeIgniter4 Installation Method
Composer (using codeigniter4/appstarter
)
Which operating systems have you tested for this bug?
Linux
Which server did you use?
apache
Database
MySQL
What happened?
The model fails to save entities when they have internal entities. This occurs because of an error in the code in 'system\Model.php'. Specifically in the function objectToRawArray.
When "parent::objectToRawArray($data, $onlyChanged);
" is called in that function, the parameter "$recursive
" is missing.
/**
* Takes a class an returns an array of it's public and protected
* properties as an array with raw values.
*
* @param object|string $data
* @param bool $recursive If true, inner entities will be casted as array as well
*
* @throws ReflectionException
*
* @return array|null Array
*/
protected function objectToRawArray($data, bool $onlyChanged = true, bool $recursive = false): ?array
{
$properties = parent::objectToRawArray($data, $onlyChanged);
// Always grab the primary key otherwise updates will fail.
if (
method_exists($data, 'toRawArray') && (! empty($properties) && ! empty($this->primaryKey) && ! in_array($this->primaryKey, $properties, true)
&& ! empty($data->{$this->primaryKey}))
) {
$properties[$this->primaryKey] = $data->{$this->primaryKey};
}
return $properties;
}
Steps to Reproduce
$entityFather = new FatherEntity();
$entityFather->name = 'Patricio';
$entityFather->sonEntity = new SonEntity([ 'name' => 'Maximo', 'age' => 18 ]);
// Get the raw array with $onlyChanged = false and $recursive = true
$rawArray = $entityFather->toRawArray(false,true);
// $rawArray is now:
Array(
'name' => 'Patricio',
'sonEntity' => SonEntity Object (
....
...
)
)
Expected Output
// $rawArray should be
Array(
'name' => 'Patricio',
'sonEntity' => Array(
'name' => 'Maximo',
'age' => '18',
)
)
Anything else?
Thank you very much for your work.
Duplicate of #4360 ?
Maybe need to revive #4368? @najdanovicivan are you available/interested to complete that?
@MGatner Iām willing to work on this one but Iām currently on vacation. I wont be able to start working on this one before the September 5th
Not a problem! We're all volunteers doing what we can when we can š¤ If this isn't feasible for you then please speak up so others will take it on - no judgement whatsoever (I haven't written code for CI4 in months).
Does this issue has been solved at current develop branch?
Because I can't reproduce the output by using the same Steps to Reproduce
code.
This is the output what I get, as this image shows that the type of sonEntity is array
without SonEntity Object
.
Does this issue has been solved at current develop branch?
Yes, I cannot reproduce the issue.
On develop
branch.
<?php
namespace App\Controllers;
use CodeIgniter\Entity\Entity;
class Home extends BaseController
{
public function index()
{
$entityFather = new Entity();
$entityFather->name = 'Patricio';
$entityFather->sonEntity = new Entity(['name' => 'Maximo', 'age' => 18]);
$rawArray = $entityFather->toRawArray(false, true);
dd($rawArray);
}
}
$ php public/index.php
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā $rawArray ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
array (2) [
'name' => string (8) "Patricio"
'sonEntity' => array (2) [
'name' => string (6) "Maximo"
'age' => integer 18
]
]
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
Called from .../app/Controllers/Home.php:18 [dd()]
The model fails to save entities when they have internal entities. This occurs because of an error in the code in
system\Model.php
. Specifically in the functionobjectToRawArray()
. When "parent::objectToRawArray($data, $onlyChanged);
" is called in that function, the parameter "$recursive
" is missing.
https://github.com/codeigniter4/CodeIgniter4/blob/6234f0ed45c66da356f88255e831336978b3127d/system/Model.php#L706-L708
I don't know this is a bug or not.
I don't know this is a bug or not.
Looks like it to me!