title: How to handle SQL triggers in Symfony2?
body: <p>I have a <code>User</code> entity in my Symfony2/Doctrine2 webapp. This user has an attribute <code>last_updated</code> to identify the latest time, anything has changed. I set this attribute to <code>NOT NULL</code> in my database. So far, so good.</p>
  
  <p>I would consider it to be good practice to create a SQL trigger in the database, that sets this <code>last_updated</code> to <code>NOW()</code> on every <code>INSERT</code> or <code>UPDATE</code>. So you don't have to care about this in your application. So that's what I did, I implemented this trigger in my database.</p>
  
  <p>But if I now create a user in my app</p>
  
  <pre><code>$user = new User();
  $em = $this-&gt;getDoctrine()-&gt;getManager();
  $em-&gt;persist($user);
  $em-&gt;flush();
  </code></pre>
  
  <p>I get an error message by Symfony:</p>
  
  <blockquote>
    <p>An exception occurred while executing <code>'INSERT INTO User (username, ..., last_updated) VALUES (?, ..., ?)'</code>
    with params <code>["johndoe", ..., null]:</code></p>
    
    <p>SQLSTATE[23000]: Integrity constraint violation: 1048 Column <code>'last_updated'</code> cannot be null</p>
  </blockquote>
  
  <p>The problem is clear: Symfony is trying to fire an <code>INSERT</code>-statement to the database with the parameter <code>null</code> for <code>last_updated</code>, which is not allowed - as this attribute may not be null.</p>
  
  <p>I could quickly think of two workarounds:</p>
  
  <ul>
  <li>One workaround would be to take the <code>last_updated</code> field out of the entity description. Then Symfony would not try to pass anything to the database for this column, and the trigger would set the appropriate value. But I don't think this is a good way, because as soon as I would try to update the db schema (<code>doctrine:schema:update --force</code>) I would loose my <code>last_updated</code>-column.</li>
  <li>Another workaround: Simply do <code>$user-&gt;setLastUpdated(new \DateTime())</code> before I <code>persist()</code> and <code>flush()</code>. But this would minimize the advantage of using a trigger on my database to avoid having to care about it in my application.</li>
  </ul>
  
  <p>Is there any way to let Symfony/Doctrine know that there is a trigger running on my database? If not, (how) can I hook into Symfony/Doctrine to implement a proper workaround?</p>