Skip to content

Issue deleting an referenced domain object from an array field #11

@michaeldewildt

Description

@michaeldewildt

Righto lets say I have a BlogPost DomainObject that contains an ArrayField of Comments as follows.

class Comment extends Moa\DomainObject
{

}

class Post extends Moa\DomainObject
{
    public function properties()
    {
        return array(
            'comments' => new Moa\Types\ArrayField(array(
                'type' => new Moa\Types\ReferenceField(array(
                    'type' => 'Comment'
                )),
            )),
        );
    }
}

There is an issue where if I delete a comment from within that referenced field the post does not know it is gone until the LazyProperty is consumed in some way. For example:

Comment::remove(array('_id' => new \MongoId('someCommentId')));

$post = Post::findOne(array('_id' => new \MongoId('somePostId')));

echo count($post->comments); //prints 1 which is incorrect

echo isset($post->comments[0]); //prints 1 which is incorrect

echo $post->comments[0]; //prints nothing which is correct because it's deleted

foreach ($category->comments as $comment) {
    echo $comment; //Wait what... how did you get here?            
}

The problem is the instance is not loaded until the array is referenced. It returns an array of lazy properties instead of an expected empty set that can cause some crazy bugs with iterators. Especially in twig!

I am not sure how to fix this issue without breaking lazy loading for everything else!

@lwc @alecsloman I'd be keen to hear your thoughts.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions