jeremyharris / cakephp-lazyload

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

Update `has()` to behave the same as overridden `EntityTrait::has()` #7

Closed dewalddp closed 7 years ago

dewalddp commented 7 years ago

The LazyLoadEntityTrait::has() method behaved differently than the overwritten EntityTrait::has() method. The former always returned true if the property exists for a hasMany relationship, even if there were no values. The latter returns true only if there are values.

jeremyharris commented 7 years ago

I wrote a little test case to show the previous behavior, and it returns true for an empty tags set as well.

This test will fail, and uses the original has(). Debugging the entity shows an empty set, just like with the lazy loader.

    public function testOriginalHas()
    {
        TableRegistry::clear();
        $Articles = TableRegistry::get('Articles');
        $Articles->belongsTo('Authors');
        $Articles->belongsToMany('Tags');

        $this->assertSame('\Cake\ORM\Entity', $Articles->entityClass());

        $article = $Articles->get(3, [
            'contain' => ['Authors', 'Tags']
        ]);

        $this->assertTrue($article->has('author'));
        // this next assertion fails
        $this->assertFalse($article->has('tags'));
    }
jeremyharris commented 7 years ago

@dewalddp any thoughts on this?

jeremyharris commented 7 years ago

Going to close this for now due to lack of updates.