Skip to content

Instantly share code, notes, and snippets.

@frosas
Created April 26, 2012 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frosas/2501178 to your computer and use it in GitHub Desktop.
Save frosas/2501178 to your computer and use it in GitHub Desktop.
<?php
namespace Redconvive\Bundle\Entity;
use Symfony\Component\Validator\Validator as SymfonyValidator;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\EntityManager;
class Validator
{
private $validator;
function __construct(SymfonyValidator $validator)
{
$this->validator = $validator;
}
function onFlush(OnFlushEventArgs $args)
{
foreach ($this->getEntitiesToValidate($args->getEntityManager()) as $entity) {
$violations = $this->validator->validate($entity);
if (count($violations)) {
throw new \Exception("Invalid entity ($violations)");
}
}
}
private function getEntitiesToValidate(EntityManager $entityManager)
{
return array_merge(
$entityManager->getUnitOfWork()->getScheduledEntityInsertions(),
$entityManager->getUnitOfWork()->getScheduledEntityUpdates());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment