jeremyharris / cakephp-lazyload

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

LazyLoadTrait does not respect unsetting a property [part II] #12

Closed robertpustulka closed 7 years ago

robertpustulka commented 7 years ago

Seems like #9 hasn't been fixed.

Consider this example again. This time group has been eager-loaded.

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

_lazyLoaded hasn't noted that there's group already inside an entity. Now I unset it and lazy loader loads it again.

Maybe _lazyLoaded should be notified that a property has been unset?

This issue causes TranslateBehavior association loading to be stuck in a loop as _rowMapper unset eagerly loaded association properties but lazy loader wants to load them again...

Here is my temporary fix:

    public function unsetProperty($property)
    {
        $this->_lazyLoaded[] = $property;

        return parent::unsetProperty($property);
    }

But maybe it could be done in a more elegant way.

jeremyharris commented 7 years ago

Yep, you're right about that. I'll work up a fix today. Thanks!

jeremyharris commented 7 years ago

I probably released this a bit prematurely :D

@robertpustulka Would you mind pulling the release or master and check it against your use case? I wrote a failing test before the actual fix, so I think it'll be fine, but would feel better if you checked it out as well.

robertpustulka commented 7 years ago

@jeremyharris I'll try it tomorrow :)

robertpustulka commented 7 years ago

@jeremyharris it works :)

jeremyharris commented 7 years ago

@robertpustulka fantastic, thanks for testing it!