Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Finally I got to the solution. I'm sure there are some others (like using event listeners, for example, that seems to be simpler), but right now it's the best I could find (it works, and that's what matters).</p> <p>I was trying to override the <code>createAction()</code> based on examples that I found in another forum thread, but I was getting two records in the table instead of one only. The most important thing was overriding the WHOLE action method and put the custom code in it.</p> <p><strong>Controller</strong>:</p> <pre><code>&lt;?php namespace Mercury\CargoRecognitionBundle\Controller; use Symfony\Component\Security\Core\SecurityContextInterface; use Symfony\Bridge\Monolog\Logger; use Sonata\AdminBundle\Controller\CRUDController as Controller; use Application\Sonata\UserBundle\Entity\User; use Mercury\CargoRecognitionBundle\Entity\Attachment; use Mercury\CargoRecognitionBundle\Entity\SystemUser; use Mercury\CargoRecognitionBundle\Repository\SystemUserRepository; class AttachmentAdminController extends Controller { /** * Set the system user ID */ private function updateFields($object) { $userName = $this-&gt;container-&gt;get('security.context') -&gt;getToken() -&gt;getUser() -&gt;getUsername(); $user = $this-&gt;getDoctrine() -&gt;getRepository('ApplicationSonataUserBundle:User') -&gt;findOneByUsername($userName); $object-&gt;setSystemUser($user); return $object; } /** * (non-PHPdoc) * @see Sonata\AdminBundle\Controller.CRUDController::createAction() */ public function createAction() { // the key used to lookup the template $templateKey = 'edit'; if (false === $this-&gt;admin-&gt;isGranted('CREATE')) { throw new AccessDeniedException(); } $object = $this-&gt;admin-&gt;getNewInstance(); $object = $this-&gt;updateFields($object); // custom method $this-&gt;admin-&gt;setSubject($object); $form = $this-&gt;admin-&gt;getForm(); $form-&gt;setData($object); if ($this-&gt;get('request')-&gt;getMethod() == 'POST') { $form-&gt;bindRequest($this-&gt;get('request')); $isFormValid = $form-&gt;isValid(); // persist if the form was valid and if in preview mode the preview was approved if ($isFormValid &amp;&amp; (!$this-&gt;isInPreviewMode() || $this-&gt;isPreviewApproved())) { $this-&gt;admin-&gt;create($object); if ($this-&gt;isXmlHttpRequest()) { return $this-&gt;renderJson(array( 'result' =&gt; 'ok', 'objectId' =&gt; $this-&gt;admin-&gt;getNormalizedIdentifier($object) )); } $this-&gt;get('session')-&gt;setFlash('sonata_flash_success','flash_create_success'); // redirect to edit mode return $this-&gt;redirectTo($object); } // show an error message if the form failed validation if (!$isFormValid) { $this-&gt;get('session')-&gt;setFlash('sonata_flash_error', 'flash_create_error'); } elseif ($this-&gt;isPreviewRequested()) { // pick the preview template if the form was valid and preview was requested $templateKey = 'preview'; } } $view = $form-&gt;createView(); // set the theme for the current Admin Form $this-&gt;get('twig')-&gt;getExtension('form')-&gt;setTheme($view, $this-&gt;admin-&gt;getFormTheme()); return $this-&gt;render($this-&gt;admin-&gt;getTemplate($templateKey), array( 'action' =&gt; 'create', 'form' =&gt; $view, 'object' =&gt; $object, )); } } </code></pre> <p><strong>Service for the controller</strong>:</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, "MercuryCargoRecognitionBundle:AttachmentAdmin" ] </code></pre> <p>I took the solution from the following sites: </p> <ul> <li><a href="https://groups.google.com/forum/?fromgroups#!msg/sonata-users/xs3cNFD7oO4/pJYtqHgULY8J%5B1-25%5D" rel="noreferrer">Sonata-Users</a>,</li> <li><a href="http://forum.symfony-project.org/viewtopic.php?f=23&amp;t=44695&amp;p=137924#p137924" rel="noreferrer">Symfony framework forums</a>,</li> </ul> <p>(And the Sonata Project documentation)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload