Overriding createAction in SonataAdmin

2,441 views
Skip to first unread message

Nelson Suniaga

unread,
Aug 13, 2012, 12:12:39 AM8/13/12
to sonata...@googlegroups.com
Hi everybody

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. Here is my AdminController:

<?php

namespace Mercury\CargoRecognitionBundle\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()
{
if ($this->get('request')->getMethod() == 'POST') {
$flash = $this->get('session')->getFlash('sonata_flash_success');

if (! empty($flash) && $flash == 'flash_create_success') {
#$userManager = $this->container->get('fos_user.user_manager');
#$user = $this->container->get('context.user');
#$userManager = $session->get('username');
$user = $this->container->get('security.context')->getToken()->getUser()->getUsername();

$attachment = new Attachment();
$attachment->setPath('/tmp/image.jpg');
$attachment->setNotes('nothing interesting to say');
$attachment->getSystemUser($user);

$em = $this->getDoctrine()->getEntityManager();
$em->persist($product);
$em->flush();
}
}

parent::createAction();
    }
}


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.

I really need a good tutorial about this, but seems like any information I get about this is obsolete in some aspect. 

Thanks to everybody in advanced for the help...

martin.fobian

unread,
Aug 22, 2012, 7:38:45 AM8/22/12
to sonata...@googlegroups.com
Hi there,

have you added your controller to the corresponding admin service? Like this:

services:

    sonata.admin.attachment:
        class: Mercury\CargoRecognitionBundle\Admin\AttachmentAdmin
        tags:
          - { name: sonata.admin, manager_type: orm, group: mercury_cargo_recognition, label: Attachment }
        arguments: [null, Mercury\CargoRecognitionBundle\Entity\Attachment, MercuryCargoRecognitionBundle:AttachmentAdmin]

The important part here is the third argument (on the last line), which should point to your controller class.

[On a sidenote: you probably want to return the response that parent::createAction() returns.]

Nelson Suniaga

unread,
Aug 22, 2012, 11:09:07 AM8/22/12
to sonata...@googlegroups.com
I found the error a week ago (I forgot to post the solution here). I already had a block admin service and the error was in the third argument: instead having a MercuryCargoRecognitionBundle:AttachmentAdmin I had a SonataAdminBundle:CRUD so my createAction() method was never called.

But then I got another error: doble insert in the database (because there was a double object generation)

I solved the issue overriding the whole createAction method and calling the container method just after the getNewInstance() line. It's the only way I can avoid the double object generation with the double insert in the table.

But I think the ultimate solution is to call prePersist() and preUpdate() methods in the entity class... but I haven't tested it yet.

Thanks for your replay, by the way.
Reply all
Reply to author
Forward
0 new messages