spotorm / spot2

Spot v2.x DataMapper built on top of Doctrine's Database Abstraction Layer
http://phpdatamapper.com
BSD 3-Clause "New" or "Revised" License
601 stars 101 forks source link

Question about entity validation #272

Closed FlipEverything closed 5 years ago

FlipEverything commented 5 years ago

Should the entity validation fail silently? Is this by design? For example if I have an entity with required fields like the author_id:

tests/Entity/Post.php

return [
     'author_id' => ['type' => 'integer', 'required' => true],
];

And If I do something like in tests/Relations.php

public function testHasManyRelationCountZero()
{
    $mapper = test_spot_mapper('\SpotTest\Entity\Post');
    $post = $mapper->get();
    $post->title = "No Comments";
    $post->body = "<p>Comments relation test</p>";
    $mapper->save($post);

    $this->assertSame(0, count($post->comments));
}

The $post->comments will be null and the count($post->comments) will throw an error, because the $mapper->save($post) silently fails with validation error (author_id is required).

Should someone check every save(), update(), etc. operation with $entity->hasErrors() or $result !== false or should the validation throw a ValidationError or something like that?

vlucas commented 5 years ago

Yes - this is outlined in the docs here: http://phpdatamapper.com/docs/crud/

FlipEverything commented 5 years ago

OK, thank you! I missed that.