codeigniter4 / CodeIgniter4

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
5.3k stars 1.9k forks source link

Bug: Entity::toRawArray incorrect work. #4360

Open iRedds opened 3 years ago

iRedds commented 3 years ago

I believe that the behavior of Entity::toRawArray is not correct and not fully tested. For example, I'll take a test https://github.com/codeigniter4/CodeIgniter4/blob/4cbfe2cd25ff8b08eee323cc734a2113cead8eb1/tests/system/EntityTest.php#L773-L792

If the toRawArray() method is called with arguments $onlyChanged = true and $recursive = true Then the result of execution will be

[
    'entity' => []
]

instead of

[
    'entity' => [
    'foo'        => null, 
    'bar'        => null, 
    'default'    => 'sumfin', 
    'created_at' => null, 
   ]
]
najdanovicivan commented 3 years ago

I can confirm this is bug. When I initially added the $recursive parameter I did not take into consideration that hasChanged method have to be updated as well.

We need deffiniton on how this is supposed to work.

If there is a single change inside entity object. ToRawArray() method on the inner entity might have to be called with $onlyChanged = false

Most of NoSQL are going to replace the complete value with the new object instead of updating just modified values. There are means of updating the nested values but those differ a lot per DB.

So I'd go with toRawArray doing replace of complete inner object. If there is a need for implementing the update on the inner model those can always be handled in the Model by doing non recursive toRawArray() and manually handling the inner entities.

I can go ahead and start working on fixing this one.