sonata-project / SonataAdminBundle

The missing Symfony Admin Generator
https://docs.sonata-project.org/projects/SonataAdminBundle
MIT License
2.11k stars 1.26k forks source link

set my user id when create new record #703

Closed Raulken closed 11 years ago

Raulken commented 12 years ago

when i create new post with sonata i want that recorder my user_id

i try to use ->add('user')

but return select list of all users

help plz

vhf commented 11 years ago

Add this to your admin controller's use : use Symfony\Component\DependencyInjection\ContainerInterface;

and make your controller's constructor look like this : public function __construct($code, $class, $baseControllerName, ContainerInterface $container) { parent::__construct($code, $class, $baseControllerName); $this->container = $container; }

then you can you use prePersist : public function prePersist($post) { $current_user = $this->container->get('security.context')->getToken()->getUser(); $post->setUser($current_user); }

(see here : http://tech.dupeu.pl/2012/12/symfony-2-1-customizing-sonataadminbundle/ )

rande commented 11 years ago

You can also overwrite the getNewInstance method

Chrysweel commented 11 years ago

I want add the User logged. But with this solution I get the following error:

Cannot import resource "/var/www/workspace/myproject/app/config/." from "/var/www/workspace/myproject/app/config/routing.yml".

ErrorException: Catchable Fatal Error: Argument 4 passed to ircd\ManageBundle\Admin\BannedAdmin::__construct() must implement interface Symfony\Component\DependencyInjection\ContainerInterface, none given, called in /var/www/workspace/myproject/app/cache/dev/appDevDebugProjectContainer.php on line 3896 and defined in /var/www/workspace/myproject/src/ircd/ManageBundle/Admin/BannedAdmin.php line 51

@vhf Any idea ?? I have symfony 2.2.1 and my composer.json is:

 "php": ">=5.3.3",
        "symfony/symfony": "2.2.*",
        "doctrine/orm": "~2.2,>=2.2.3",
        "doctrine/doctrine-bundle": "1.2.*",
        "twig/extensions": "1.0.*",
        "symfony/assetic-bundle": "2.1.*",
        "symfony/swiftmailer-bundle": "2.2.*",
        "symfony/monolog-bundle": "2.2.*",
        "sensio/distribution-bundle": "2.2.*",
        "sensio/framework-extra-bundle": "2.2.*",
        "sensio/generator-bundle": "2.2.*",
        "jms/security-extra-bundle": "1.4.*",
        "jms/di-extra-bundle": "1.3.*",        

        "friendsofsymfony/user-bundle": "1.3.*",
        "sonata-project/block-bundle" : "2.2.*@dev",
        "sonata-project/cache-bundle" : "dev-master",
        "sonata-project/admin-bundle": "2.2.*@dev",
        "knplabs/knp-menu-bundle": "1.1.*@dev",
        "sonata-project/doctrine-orm-admin-bundle": "2.2.*@dev",
        "sonata-project/user-bundle": "2.2.*@dev",
        "friendsofsymfony/rest-bundle": "0.12.*@dev",
        "jms/serializer-bundle": "0.12.*@dev"

@rande could you reopen this issue? thanks!

Chrysweel commented 11 years ago

One solution is this:

sonata.banned_admin:
        class: myproject\AcmeBundle\Admin\BannedAdmin
        tags:
            - { name: sonata.admin, manager_type: orm, group: 'Ircd', label: Banned}
        arguments:
            - null
            - myproject\AcmeBundle\Entity\Banned
            - SonataAdminBundle:CRUD
            - @service_container
``
But I think it is not the right way... Include the service_container... I dont know...