使用Sonata Admin在编程创建的对象上更新ACL

n3o*_*3on 7 symfony sonata-admin

我正在使用带有ACL的Sonata-Admin软件包,但我必须以编程方式创建一些对象.但我无法弄清楚如何正确更新已创建实体的ACL表.所以我总是要执行

php app/console sonata:admin:generate-object-acl

这当然不是一个永久的解决方案.

我尝试过这样描述:http://symfony.com/doc/current/cookbook/security/acl.html#creating-an-acl-and-adding-an-ace所以我在我的实体中实现了DomainObjectInterface,添加了getObjectIdentifier方法.

但是现在我在执行时遇到Symfony\Component\Security\Acl\Exception\AclAlreadyExistsException异常:

php app/console sonata:admin:generate-object-acl

所以我猜这不是使用sonata-admin时这样做的正确方法.但我在文档中找不到任何内容.

n3o*_*3on 2

好吧,我花了一些时间再调试一下,我想我找到了一个很好的解决方案:

获取要创建的对象的管理类:

$whateverAdmin = $this->get('app.admin.whatever');

//create the object
$whatever = new Whatever();
$whatever->setName('test');
Run Code Online (Sandbox Code Playgroud)

现在使用管理类来创建对象:

$whateverAdmin->create($whatever);
Run Code Online (Sandbox Code Playgroud)

或者,如果您想使用实体管理器来持久化,您只需使用管理类更新 ACL:

$em->persist($whatever);
$em->flush(); // important to flush first so an ID ist generated

$whateverAdmin->createObjectSecurity($whatever);
Run Code Online (Sandbox Code Playgroud)