Advertisement
ssmusoke

Base Record

Mar 3rd, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.06 KB | None | 0 0
  1. # symfony validator service classes
  2. use Doctrine\ORM\Mapping as ORM;  
  3. use Symfony\Component\Validator\Validator;
  4. use Symfony\Component\Validator\ValidatorFactory;
  5.  
  6. /**
  7.  * Base class which contains commonly used funcitons, as of Doctrine2 it does not need to extend any Doctrine specific class
  8.  *
  9.  * @ORM\MappedSuperclass
  10.  * @ORM\HasLifecycleCallbacks
  11.  *
  12.  */
  13. class BaseRecord {
  14.  
  15.  
  16.     /**
  17.      * Hook to validate entities before they are persisted to the database, will not throw exceptions
  18.      * but will update the error stack as Doctrine1 did
  19.      *
  20.      * @return bool
  21.      *
  22.      * @ORM\PrePersist
  23.      * @ORM\PreUpdate
  24.      */
  25.     public function validate(){
  26.         $factory = ValidatorFactory::buildDefault();
  27.         $validator = $factory->getValidator();
  28.         $violations = $validator->validate($this);
  29.         debugMessage($violations);
  30.        
  31.         if ($violations->count() > 0) {
  32.             // add the violation messages to the errors array, they will be translated
  33.             foreach ($violations as $violation) {
  34.                 $this->errorstack[] = $violaton->getMessage();
  35.             }
  36.         }
  37.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement