I have difficulties integrating the Symfony validator component with Doctrine (standalone, not using the complete Symfony framework). So far I have managed to register the annotation contraints in Doctrine's AnnotationRegistry and hook into the lifecycle callbacks, but how would I actually retrieve a validator and parse the annotations in the lifecycle method?
My entity looks like this:
<?php
namespace App\Model;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* User
*
* @ORM\HasLifecycleCallbacks
* @ORM\Table(name="users", uniqueConstraints={@ORM\UniqueConstraint(name="id_UNIQUE", columns={"id"}), @ORM\UniqueConstraint(name="username_UNIQUE", columns={"username"})})
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class User extends AbstractEntity
{
/**
* @ORM\PreUpdate
* @ORM\PrePersist
*/
public function validate()
{
// how do I retrieve the validator?
// $validator->validate($this);
}
/**
* @var string
*
* @ORM\Column(name="username", type="string", length=45, nullable=false)
* @Assert\Length(min=3, max=45)
*/
protected $username;
/** ... */
}