symfony - 使用 Sonata Admin 更新以编程方式创建的对象的 ACL

标签 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



所以我想这不是使用奏鸣曲管理员的正确方法。但是我在文档中找不到任何内容。

最佳答案

好的,我花了一些时间进行更多的调试,我想我找到了一个很好的解决方案:

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

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

//create the object
$whatever = new Whatever();
$whatever->setName('test');

现在使用 admin 类来创建对象:

$whateverAdmin->create($whatever);

或者,如果您想使用 entityManager 来持久化,您只需使用 admin 类更新 ACL:

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

$whateverAdmin->createObjectSecurity($whatever);

关于symfony - 使用 Sonata Admin 更新以编程方式创建的对象的 ACL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30488874/

相关文章:

symfony - Composer 太老了

symfony - 如何检查Twig/Symfony2中是否存在翻译项目?

php - Symfony 4.2 如何为测试公开服务

validation - 将自定义验证规则添加到 Sonata User Bundle

php - SonataAdminBundle : Redirect to list view doesn't save pagination

php - 创建用户后无法生成 "page_slug"的 URL

Symfony 和 PhpUnit 内存泄漏

javascript - 如何重写javascript库函数?

php - “The block type sonata.Admin.block.admin_list does not exist”

symfony - 更改 Sonata Admin 上的左侧菜单标签