I'm using the Sonata-Admin bundle with ACL, but I have to create some objects programmatically. But I can't figure out how I properly update the ACL tables for the created entity. So I always have to execute
php app/console sonata:admin:generate-object-acl
which is of course not a permanent solution.
I tried doing it like described here: http://symfony.com/doc/current/cookbook/security/acl.html#creating-an-acl-and-adding-an-ace so I implement the DomainObjectInterface in my entity and added the getObjectIdentifier method.
But now I'm getting an Symfony\Component\Security\Acl\Exception\AclAlreadyExistsException exception when executing:
php app/console sonata:admin:generate-object-acl
So I guess that's not the proper way to do it when using sonata-admin. But I can't find anything in the docs.
Ok, I took some time to debug a little more and I think I found a good solution:
Get the admin class of the object you want to create:
$whateverAdmin = $this->get('app.admin.whatever');
//create the object
$whatever = new Whatever();
$whatever->setName('test');
And now use the admin class to create the object:
$whateverAdmin->create($whatever);
Or if you want to use the entityManager to persist you could just update the ACL with the admin class:
$em->persist($whatever);
$em->flush(); // important to flush first so an ID ist generated
$whateverAdmin->createObjectSecurity($whatever);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With