title: Symfony2: Overriding createAction() in SonataAdmin
body: <p>I've been googling as crazy the last days trying to figure out (with no success) how override a SonataAdmin action to capture the session username and save it in the foreign key field. </p>
  
  <p>AttachmentAdminController class:</p>
  
  <pre><code>&lt;?php
  
  namespace Application\Sonata\UserBundle\Controller;
  
  use Sonata\AdminBundle\Controller\CRUDController as Controller;
  #use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use FOS\UserBundle\Entity\User;
  use Symfony\Component\Security\Core\SecurityContextInterface;
  use Symfony\Bridge\Monolog\Logger;
  use Mercury\CargoRecognitionBundle\Entity\Attachment;
  
  class AttachmentAdminController extends Controller
  {
      /**
       * (non-PHPdoc)
       * @see Sonata\AdminBundle\Controller.CRUDController::createAction()
       */
      public function createAction()
      {
          $result = parent::createAction();
  
          if ($this-&gt;get('request')-&gt;getMethod() == 'POST')
          {
              $flash = $this-&gt;get('session')-&gt;getFlash('sonata_flash_success');
  
              if (!empty($flash) &amp;&amp; $flash == 'flash_create_success')
              {
                  #$userManager = $this-&gt;container-&gt;get('fos_user.user_manager');
                  #$user = $this-&gt;container-&gt;get('context.user');
                  #$userManager = $session-&gt;get('username');
                  $user = $this-&gt;container-&gt;get('security.context')-&gt;getToken()-&gt;getUser()-&gt;getUsername();
  
                  $attachment = new Attachment();
                  $attachment-&gt;setPath('/tmp/image.jpg');
                  $attachment-&gt;setNotes('nothing interesting to say');
                  $attachment-&gt;getSystemUser($user);
  
                  $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
                  $em-&gt;persist($product);
                  $em-&gt;flush();
              }
          }
  
          return $result;
      }
  }
  </code></pre>
  
  <p>service attachment:</p>
  
  <pre><code>mercury.cargo_recognition.admin.attachment:
      class: Mercury\CargoRecognitionBundle\Admin\AttachmentAdmin
      tags:
          - { name: sonata.admin, manager_type: orm, group: General, label: Attachments }
      arguments: [ null, Mercury\CargoRecognitionBundle\Entity\Attachment, "SonataAdminBundle:CRUD" ]
  </code></pre>
  
  <p>Seems to me as the actionController() is been ignored by SonataAdminBundle (and maybe the whole class file), because there's not error messages at all, but I don't know why. Actually, I'm not sure if I'm fetching the username from the session.</p>
  
  <p>I really need a good tutorial about this, but seems like any information I get about this is obsolete in some aspect. By the way, I'm using Symfony 2.0.16</p>