title: Symfony2 onAuthenticationSuccess handler and Doctrine2 exception
body: <p>All!
  Could someone help me with Symfony2 question?
  I'd like to save into the database all the events (request related data) regarding user login [success, failure] and logout [success]. Implementation and exception below.</p>
  
  <p>Here is the config.yml</p>
  
  <pre><code>services:
  
  login_success_handler:
      class:  Boomster\LibraryBundle\Handler\LoginSuccessHandler
      arguments:  [@security.context, @doctrine]
  
  login_failure_handler:
      class:  Boomster\LibraryBundle\Handler\LoginFailureHandler
      arguments:  [@security.context, @doctrine]
  
  logout_success_handler:
      class:  Boomster\LibraryBundle\Handler\LogoutSuccessHandler
      arguments:  [@security.context, @doctrine]
  </code></pre>
  
  <p>Here is firewall security.yml</p>
  
  <pre><code>firewalls:
      secured_area:
          pattern: ^/.*
          form_login:
              check_path: auth_check
              login_path: auth_login
              success_handler: login_success_handler
              failure_handler: login_failure_handler
          logout:
              path:   auth_logout
              target: site_index
              success_handler: logout_success_handler
          anonymous: ~
  </code></pre>
  
  <p>Here is LoginSuccessHandler handler:</p>
  
  <pre><code>&lt;?php
  
  namespace Boomster\LibraryBundle\Handler;
  
  use Boomster\LibraryBundle\Entity\Visit;
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Component\HttpFoundation\Response;
  use Symfony\Component\HttpFoundation\RedirectResponse;
  use Symfony\Component\Security\Core\SecurityContext;
  use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
  use Doctrine\Bundle\DoctrineBundle\Registry as Doctrine;
  
  class LoginSuccessHandler implements AuthenticationSuccessHandlerInterface
  {
  private $doctrine;
  private $securityContext;
  
  public function __construct(SecurityContext $securityContext, Doctrine $doctrine)
  {
      $this-&gt;doctrine = $doctrine;
      $this-&gt;securityContext = $securityContext;
  }
  
  public function onAuthenticationSuccess(Request $request, TokenInterface $token)
  {
      $session = $request-&gt;getSession();
  
      if ($session-&gt;has('referer')) {
          if ($session-&gt;get('referer') !== null &amp;&amp; $session-&gt;get('referer') !== '')
          {
              $response = new RedirectResponse($session-&gt;get('referer'));
          } else {
              $response = new RedirectResponse($request-&gt;getBasePath() . '/');
          }
      } else {
          $response = new RedirectResponse($request-&gt;getBasePath() . '/');
      }
  
      if ($request-&gt;isXmlHttpRequest() || $request-&gt;request-&gt;get('_format') === 'json') {
          $response = new Response(json_encode(array('status' =&gt; 'success')));
          $response-&gt;headers-&gt;set('Content-Type', 'application/json');
      }
  
  
      $em = $this-&gt;doctrine-&gt;getManager();
      $repository = $em-&gt;getRepository('BoomsterLibraryBundle:User');
      $user = $repository-&gt;findOneByUsername($token-&gt;getUsername());
  
      $visit = new Visit();
      $visit-&gt;setFormat($request-&gt;request-&gt;get('_format', 'none'));
      $visit-&gt;setClientIp($request-&gt;request-&gt;get('client-ip', '0.0.0.0'));
      $visit-&gt;setStatus(Visit::SUCCESS);
  
      // ...
      $user-&gt;addVisit($visit);
  
      $em-&gt;persist($visit);
      $em-&gt;flush();
  
      return $response;
  }
  }
  </code></pre>
  
  <p>And finally, relations between entities:
  Entity User</p>
  
  <pre><code>class User .....
  /**
   * @var \Doctrine\Common\Collections\Collection
   *
   * @ORM\OneToMany(targetEntity="Visit", mappedBy="user", cascade={"remove"})
   *
   * @var ArrayCollection $visits;
   **/
  private $visits;
  
  /**
   * @param Visit $visit
   * @return User $user
   */
  public function addVisit($visit)
  {
      $this-&gt;visits[] = $visit;
      $visit-&gt;setUser($this);
  
      return $this;
  }
  
  /**
   * Remove visits
   *
   * @param Visit $visit
   * @return void param \Boomster\LibraryBundle\Entity\Visit $visits
   */
  public function removeVisit(Visit $visit)
  {
      $this-&gt;visits-&gt;removeElement($visit);
  }
  
  /**
   * @return \Doctrine\Common\Collections\Collection
   */
  public function getVisits()
  {
      return $this-&gt;visits-&gt;toArray();
  }
  }
  </code></pre>
  
  <p>Entity Visit</p>
  
  <pre><code>class Visit ...
  /**
   * @var User $user;
   *
   * @ORM\ManyToOne(targetEntity="User", inversedBy="visits")
   * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
   *
   */
  private $user;
  
  // getters &amp; setters for the $user skipped
  </code></pre>
  
  <p>So, the exception:</p>
  
  <p>An exception occurred while executing 'UPDATE users SET password = ?, salt = ?, updated = ? WHERE id = ?' with params [null, null, "2013-10-18 16:56:48", "141736"]:</p>
  
  <p>SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "password" violates not-null constraint </p>
  
  <p>As I understand Doctrine trying to update users table, but fields password and salt are null...</p>
  
  <p>Here is the log file:</p>
  
  <pre><code>[2013-10-18 20:47:32] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest". [] []
  [2013-10-18 20:47:32] doctrine.DEBUG: SELECT t0.id AS id1, t0.nickname AS nickname2, t0.username AS username3, t0.password AS password4, t0.email AS email5, t0.gender AS gender6, t0.website AS website7, t0.skype AS skype8, t0.salt AS salt9, t0.code AS code10, t0.status AS status11, t0.created AS created12, t0.updated AS updated13, t0.expired AS expired14 FROM users t0 WHERE t0.username = ? LIMIT 1 ["admin"] []
  [2013-10-18 20:47:32] doctrine.DEBUG: SELECT t0.id AS id1, t0.name AS name2, t0.role AS role3, t0.created AS created4, t0.updated AS updated5 FROM roles t0 INNER JOIN role_user ON t0.id = role_user.role_id WHERE role_user.user_id = ? ["141736"] []
  [2013-10-18 20:47:32] security.INFO: User "admin" has been authenticated successfully [] []
  [2013-10-18 20:47:32] doctrine.DEBUG: SELECT t0.id AS id1, t0.nickname AS nickname2, t0.username AS username3, t0.password AS password4, t0.email AS email5, t0.gender AS gender6, t0.website AS website7, t0.skype AS skype8, t0.salt AS salt9, t0.code AS code10, t0.status AS status11, t0.created AS created12, t0.updated AS updated13, t0.expired AS expired14 FROM users t0 WHERE t0.username = ? LIMIT 1 ["admin"] []
  [2013-10-18 20:47:32] doctrine.DEBUG: SELECT NEXTVAL('seq_visit') [] []
  [2013-10-18 20:47:32] doctrine.DEBUG: "START TRANSACTION" [] []
  [2013-10-18 20:47:32] doctrine.DEBUG: INSERT INTO visit (id, format, client_ip, status, created, user_id) VALUES (?, ?, ?, ?, ?, ?) {"1":18,"2":"none","3":"0.0.0.0","4":"SUCCESS","5":"2013-10-18 20:47:32","6":"141736"} []
  [2013-10-18 20:47:32] doctrine.DEBUG: UPDATE users SET password = ?, salt = ?, updated = ? WHERE id = ? ["NULL","NULL","2013-10-18 20:47:32","141736"] []
  [2013-10-18 20:47:32] doctrine.DEBUG: "ROLLBACK" [] []
  [2013-10-18 20:47:32] event.DEBUG: Notified event "kernel.exception" to listener "Symfony\Component\Security\Http\Firewall\ExceptionListener::onKernelException". [] []
  [2013-10-18 20:47:32] event.DEBUG: Notified event "kernel.exception" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelException". [] []
  [2013-10-18 20:47:32] event.DEBUG: Notified event "kernel.exception" to listener "Symfony\Component\HttpKernel\EventListener\ExceptionListener::onKernelException". [] []
  [2013-10-18 20:47:32] request.CRITICAL: Uncaught PHP Exception Doctrine\DBAL\DBALException: "An exception occurred while executing 'UPDATE users SET password = ?, salt = ?, updated = ? WHERE id = ?' with params [null, null, "2013-10-18 20:47:32", "141736"]:
  
  SQLSTATE[23502]: Not null violation: 7 ERROR:  null value in column "password" violates not-null constraint" at /data/system/webhosts/boomster.lo/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php line 47 {"exception":"[object] (Doctrine\\DBAL\\DBALException: An exception occurred while executing 'UPDATE users SET password = ?, salt = ?, updated = ? WHERE id = ?' with params [null, null, \"2013-10-18 20:47:32\", \"141736\"]:\n\nSQLSTATE[23502]: Not null violation: 7 ERROR:  null value in column \"password\" violates not-null constraint at /data/system/webhosts/boomster.lo/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php:47, PDOException: SQLSTATE[23502]: Not null violation: 7 ERROR:  null value in column \"password\" violates not-null constraint at /data/system/webhosts/boomster.lo/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:784)"} []
  </code></pre>
  
  <p>The key point:</p>
  
  <pre><code>[2013-10-18 20:47:32] doctrine.DEBUG: UPDATE users SET password = ?, salt = ?, updated = ? WHERE id = ? ["NULL","NULL","2013-10-18 20:47:32","141736"] []
  </code></pre>
  
  <p>.......... ???</p>
  
  <p>In additional, I've changed $user->addVisit() to $visit->setUser($user). Nothing changed... Here is the detailed log:</p>
  
  <pre><code>[2013-10-18 21:11:33] doctrine.DEBUG: SELECT t0.id AS id1, t0.nickname AS nickname2, t0.username AS username3, t0.password AS password4, t0.email AS email5, t0.gender AS gender6, t0.website AS website7, t0.skype AS skype8, t0.salt AS salt9, t0.code AS code10, t0.status AS status11, t0.created AS created12, t0.updated AS updated13, t0.expired AS expired14 FROM users t0 WHERE t0.username = ? LIMIT 1 ["admin"] []
  [2013-10-18 21:11:33] doctrine.DEBUG: SELECT t0.id AS id1, t0.name AS name2, t0.role AS role3, t0.created AS created4, t0.updated AS updated5 FROM roles t0 INNER JOIN role_user ON t0.id = role_user.role_id WHERE role_user.user_id = ? ["141736"] []
  [2013-10-18 21:11:33] security.INFO: User "admin" has been authenticated successfully [] []
  [2013-10-18 21:11:33] doctrine.DEBUG: SELECT t0.id AS id1, t0.nickname AS nickname2, t0.username AS username3, t0.password AS password4, t0.email AS email5, t0.gender AS gender6, t0.website AS website7, t0.skype AS skype8, t0.salt AS salt9, t0.code AS code10, t0.status AS status11, t0.created AS created12, t0.updated AS updated13, t0.expired AS expired14 FROM users t0 WHERE t0.username = ? LIMIT 1 ["admin"] []
  [2013-10-18 21:11:33] doctrine.DEBUG: SELECT NEXTVAL('seq_visit') [] []
  [2013-10-18 21:11:33] doctrine.DEBUG: "START TRANSACTION" [] []
  [2013-10-18 21:11:33] doctrine.DEBUG: INSERT INTO visit (id, format, client_ip, status, created, user_id) VALUES (?, ?, ?, ?, ?, ?) {"1":19,"2":"none","3":"0.0.0.0","4":"SUCCESS","5":"2013-10-18 21:11:33","6":"141736"} []
  [2013-10-18 21:11:33] doctrine.DEBUG: UPDATE users SET password = ?, salt = ?, updated = ? WHERE id = ? ["NULL","NULL","2013-10-18 21:11:33","141736"] []
  [2013-10-18 21:11:33] doctrine.DEBUG: "ROLLBACK" [] []
  [2013-10-18 21:11:33] event.DEBUG: Notified event "kernel.exception" to listener "Symfony\Component\Security\Http\Firewall\ExceptionListener::onKernelException". [] []
  [2013-10-18 21:11:33] event.DEBUG: Notified event "kernel.exception" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelException". [] []
  [2013-10-18 21:11:33] event.DEBUG: Notified event "kernel.exception" to listener "Symfony\Component\HttpKernel\EventListener\ExceptionListener::onKernelException". [] []
  [2013-10-18 21:11:33] request.CRITICAL: Uncaught PHP Exception Doctrine\DBAL\DBALException: "An exception occurred while executing 'UPDATE users SET password = ?, salt = ?, updated = ? WHERE id = ?' with params [null, null, "2013-10-18 21:11:33", "141736"]:
  
  SQLSTATE[23502]: Not null violation: 7 ERROR:  null value in column "password" violates not-null constraint" at /data/system/webhosts/boomster.lo/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php line 47 {"exception":"[object] (Doctrine\\DBAL\\DBALException: An exception occurred while executing 'UPDATE users SET password = ?, salt = ?, updated = ? WHERE id = ?' with params [null, null, \"2013-10-18 21:11:33\", \"141736\"]:\n\nSQLSTATE[23502]: Not null violation: 7 ERROR:  null value in column \"password\" violates not-null constraint at /data/system/webhosts/boomster.lo/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php:47, PDOException: SQLSTATE[23502]: Not null violation: 7 ERROR:  null value in column \"password\" violates not-null constraint at /data/system/webhosts/boomster.lo/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:784)"} []
  </code></pre>
  
  <p>Dont understand what is going on...
  Ideas?</p>