jeremyharris / cakephp-lazyload

A lazy loader for CakePHP entities.
MIT License
61 stars 10 forks source link

LazyLoadTrait does not respect unsetting a property. #9

Closed robertpustulka closed 7 years ago

robertpustulka commented 7 years ago

Consider following example:

Users belongsTo Groups

$user = $users->find()->first(); //$user does not have a group loaded but uses LazyLoadTrait
$user->get('group'); //this will load a group into user
$user->unsetProperty('group');
$user->get('group') //this will load a group again

It could seem like a desired behaviour but consider this:

$user = $users->find()->first();
$user->get('group');
$user->unsetProperty('group');
$users->save($user, ['associated' => 'Groups']); 

I expect that my Groups association will be removed from a DB. But lazy loader will load a group once again before saving an entity. I tried also unsetting a group_id property $user->unsetProperty('group_id'); but this fails too.

I think that a dirty() check could fix the issue.