title: Symfony2 ORM prePersist Not Working
body: <p>prePersist() method of my entity is not being invoked.</p>
  
  <pre><code>&lt;?php
  
  namespace Acme\DemoBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * Users
   *
   * @ORM\Table(name="users")
   * @ORM\Entity
   * @ORM\HasLifecycleCallbacks()
   */
  class Users
  {
      /**
       * @var string
       *
       * @ORM\Column(name="username", type="string", length=255, nullable=false)
       */
      private $username;
  
      /**
       * @var string
       *
       * @ORM\Column(name="firstname", type="string", length=255, nullable=false)
       */
      private $firstname;
  
      /**
       * @var string
       *
       * @ORM\Column(name="lastname", type="string", length=255, nullable=false)
       */
      private $lastname;
  
      /**
       * @var string
       *
       * @ORM\Column(name="email", type="string", length=255, nullable=false)
       */
      private $email;
  
      /**
       * @var string
       *
       * @ORM\Column(name="password", type="string", length=32, nullable=false)
       */
      private $password;
  
      /**
       * @var boolean
       *
       * @ORM\Column(name="active", type="boolean", nullable=false)
       */
      private $active;
  
      /**
       * @var \DateTime
       *
       * @ORM\Column(name="created", type="datetime", nullable=false)
       */
      private $created;
  
      /**
       * @var \DateTime
       *
       * @ORM\Column(name="modified", type="datetime", nullable=false)
       */
      private $modified;
  
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="IDENTITY")
       */
      private $id;
  
  
  
      /**
       * Set username
       *
       * @param string $username
       * @return Users
       */
      public function setUsername($username)
      {
          $this-&gt;username = $username;
  
          return $this;
      }
  
      /**
       * Get username
       *
       * @return string 
       */
      public function getUsername()
      {
          return $this-&gt;username;
      }
  
      /**
       * Set firstname
       *
       * @param string $firstname
       * @return Users
       */
      public function setFirstname($firstname)
      {
          $this-&gt;firstname = $firstname;
  
          return $this;
      }
  
      /**
       * Get firstname
       *
       * @return string 
       */
      public function getFirstname()
      {
          return $this-&gt;firstname;
      }
  
      /**
       * Set lastname
       *
       * @param string $lastname
       * @return Users
       */
      public function setLastname($lastname)
      {
          $this-&gt;lastname = $lastname;
  
          return $this;
      }
  
      /**
       * Get lastname
       *
       * @return string 
       */
      public function getLastname()
      {
          return $this-&gt;lastname;
      }
  
      /**
       * Set email
       *
       * @param string $email
       * @return Users
       */
      public function setEmail($email)
      {
          $this-&gt;email = $email;
  
          return $this;
      }
  
      /**
       * Get email
       *
       * @return string 
       */
      public function getEmail()
      {
          return $this-&gt;email;
      }
  
      /**
       * Set password
       *
       * @param string $password
       * @return Users
       */
      public function setPassword($password)
      {
          $this-&gt;password = $password;
  
          return $this;
      }
  
      /**
       * Get password
       *
       * @return string 
       */
      public function getPassword()
      {
          return $this-&gt;password;
      }
  
      /**
       * Set active
       *
       * @ORM\PrePersist
       * @param boolean $active
       * @return Users
       */
      public function setActive($active)
      {
          $this-&gt;active = $active;
  
          return $this;
      }
  
      /**
       * Get active
       *
       * @return boolean 
       */
      public function getActive()
      {
          return $this-&gt;active;
      }
  
      /**
       * Set created
       *
       * @param \DateTime $created
       * @return Users
       */
      public function setCreated()
      {
          $this-&gt;created = $created;
  
          return $this;        
      }
  
      /**
       * Get created
       *
       * @return \DateTime 
       */
      public function getCreated()
      {
          return $this-&gt;created;
      }
  
      /**
       * Set modified
       *
       * @param \DateTime $modified
       * @return Users
       */
      public function setModified()
      {
          $this-&gt;modified = $modified;
  
          return $this;
      }
  
      /**
       * Get modified
       *
       * @return \DateTime 
       */
      public function getModified()
      {
          return $this-&gt;modified;
      }
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
      * @ORM\PrePersist
      * @ORM\PreUpdate
      */
      public function prePersist(){
  
        $this-&gt;active = 0;
        $this-&gt;created = date('Y-m-d H:i:s');
        $this-&gt;modified = date('Y-m-d H:i:s');
  
      }
  }
  </code></pre>
  
  <p>Here is my code of Form/UsersType.php</p>
  
  <pre><code>&lt;?php
  namespace Acme\DemoBundle\Form;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  
  
  class UsersType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('username','text', array('required' =&gt; true))
              -&gt;add('firstname','text', array('required' =&gt; true))
              -&gt;add('lastname','text', array('required' =&gt; true))
              -&gt;add('email', 'email', array('label' =&gt; 'E-Mail'))
              -&gt;add('password', 'password', array('label' =&gt; 'Password'))
              -&gt;add('password_confirmation', 'password', array('mapped' =&gt; false)) // Added virtual field on form
  /*                
              -&gt;add('active','hidden',array('data' =&gt; ''))
              -&gt;add('created','hidden',array('data' =&gt; ''))
              -&gt;add('modified','hidden',array('data' =&gt; ''))
   * 
   */
          ;        
      }
  
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'Acme\DemoBundle\Entity\Users'
          ));
      }
  
      public function getName()
      {
          return 'acme_demobundle_userstype';
      }
  }
  </code></pre>
  
  <p>I have commented out these three fields active, created and modified to not display them in the form.</p>
  
  <p>But i am getting error:</p>
  
  <pre><code>An exception occurred while executing 'INSERT INTO users (username, firstname, lastname, email, password, active, created, modified) VALUES (?, ?, ?, ?, ?, ?, ?, ?)' with params ["neeraj", "neeraj", "kumar", "neeraj.kumar@test.com", "p@ssw0rd", null, null, null]:
  </code></pre>
  
  <p>What i am missing in the Entity?</p>
  
  <p><strong>Stack Trace</strong></p>
  
  <pre><code>Stack Trace (Plain Text)  -
  
  [1] Doctrine\DBAL\DBALException: An exception occurred while executing 'INSERT INTO users (username, firstname, lastname, email, password, active, created, modified) VALUES (?, ?, ?, ?, ?, ?, ?, ?)' with params ["neeraj", "neeraj", "kumar", "neeraj.kumar@rsystems.com", "p@ssw0rd", null, null, null]:
  
  SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'active' cannot be null
      at n/a
          in C:\wamp\www\Symfony\vendor\doctrine\dbal\lib\Doctrine\DBAL\DBALException.php line 47
  
      at Doctrine\DBAL\DBALException::driverExceptionDuringQuery(object(PDOException), 'INSERT INTO users (username, firstname, lastname, email, password, active, created, modified) VALUES (?, ?, ?, ?, ?, ?, ?, ?)', array('neeraj', 'neeraj', 'kumar', 'neeraj.kumar@rsystems.com', 'p@ssw0rd', null, null, null))
          in C:\wamp\www\Symfony\vendor\doctrine\dbal\lib\Doctrine\DBAL\Statement.php line 140
  
      at Doctrine\DBAL\Statement-&gt;execute()
          in C:\wamp\www\Symfony\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\BasicEntityPersister.php line 277
  
      at Doctrine\ORM\Persisters\BasicEntityPersister-&gt;executeInserts()
          in C:\wamp\www\Symfony\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 929
  
      at Doctrine\ORM\UnitOfWork-&gt;executeInserts(object(ClassMetadata))
          in C:\wamp\www\Symfony\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 318
  
      at Doctrine\ORM\UnitOfWork-&gt;commit(null)
          in C:\wamp\www\Symfony\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php line 355
  
      at Doctrine\ORM\EntityManager-&gt;flush()
          in C:\wamp\www\Symfony\src\Acme\DemoBundle\Controller\UsersController.php line 55
  
      at Acme\DemoBundle\Controller\UsersController-&gt;createAction(object(Request))
          in  line 
  
      at call_user_func_array(array(object(UsersController), 'createAction'), array(object(Request)))
          in C:\wamp\www\Symfony\app\bootstrap.php.cache line 2774
  
      at Symfony\Component\HttpKernel\HttpKernel-&gt;handleRaw(object(Request), '1')
          in C:\wamp\www\Symfony\app\bootstrap.php.cache line 2748
  
      at Symfony\Component\HttpKernel\HttpKernel-&gt;handle(object(Request), '1', true)
          in C:\wamp\www\Symfony\app\bootstrap.php.cache line 2878
  
      at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel-&gt;handle(object(Request), '1', true)
          in C:\wamp\www\Symfony\app\bootstrap.php.cache line 2179
  
      at Symfony\Component\HttpKernel\Kernel-&gt;handle(object(Request))
          in C:\wamp\www\Symfony\web\app_dev.php line 28
  
  [2] PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'active' cannot be null
      at n/a
          in C:\wamp\www\Symfony\vendor\doctrine\dbal\lib\Doctrine\DBAL\Statement.php line 138
  
      at PDOStatement-&gt;execute(null)
          in C:\wamp\www\Symfony\vendor\doctrine\dbal\lib\Doctrine\DBAL\Statement.php line 138
  
      at Doctrine\DBAL\Statement-&gt;execute()
          in C:\wamp\www\Symfony\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\BasicEntityPersister.php line 277
  
      at Doctrine\ORM\Persisters\BasicEntityPersister-&gt;executeInserts()
          in C:\wamp\www\Symfony\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 929
  
      at Doctrine\ORM\UnitOfWork-&gt;executeInserts(object(ClassMetadata))
          in C:\wamp\www\Symfony\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 318
  
      at Doctrine\ORM\UnitOfWork-&gt;commit(null)
          in C:\wamp\www\Symfony\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php line 355
  
      at Doctrine\ORM\EntityManager-&gt;flush()
          in C:\wamp\www\Symfony\src\Acme\DemoBundle\Controller\UsersController.php line 55
  
      at Acme\DemoBundle\Controller\UsersController-&gt;createAction(object(Request))
          in  line 
  
      at call_user_func_array(array(object(UsersController), 'createAction'), array(object(Request)))
          in C:\wamp\www\Symfony\app\bootstrap.php.cache line 2774
  
      at Symfony\Component\HttpKernel\HttpKernel-&gt;handleRaw(object(Request), '1')
          in C:\wamp\www\Symfony\app\bootstrap.php.cache line 2748
  
      at Symfony\Component\HttpKernel\HttpKernel-&gt;handle(object(Request), '1', true)
          in C:\wamp\www\Symfony\app\bootstrap.php.cache line 2878
  
      at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel-&gt;handle(object(Request), '1', true)
          in C:\wamp\www\Symfony\app\bootstrap.php.cache line 2179
  
      at Symfony\Component\HttpKernel\Kernel-&gt;handle(object(Request))
          in C:\wamp\www\Symfony\web\app_dev.php line 28
  </code></pre>