0

我正在使用 Symfony2、Doctrine 和 ACL,但遇到了性能问题。

在 Twig 模板中,我需要检查集合中每个公司的当前用户角色以显示链接:

{%- if is_expr_granted("hasRole('ROLE_SUPPORT') or hasPermission(object, 'OPERATOR')", company) -%}
    <a href="configure">{{- company.name -}}</a>
{%- else -%}

我可以在 webProfiler 中看到为每个公司生成了以下 2 个查询:

SELECT a.ancestor_id
FROM acl_object_identities o 
INNER JOIN acl_classes c ON c.id = o.class_id INNER JOIN acl_object_identity_ancestors a ON a.object_identity_id = o.id
WHERE ((o.object_identifier = '154' AND c.class_type = 'St\\CoreBundle\\Entity\\Company'))`

SELECT o.id as acl_id, o.object_identifier, o.parent_object_identity_id, o.entries_inheriting, c.class_type, e.id as ace_id, e.object_identity_id, e.field_name, e.ace_order, e.mask, e.granting, e.granting_strategy, e.audit_success, e.audit_failure, s.username, s.identifier as security_identifier 
FROM acl_object_identities o
INNER JOIN acl_classes c ON c.id = o.class_id
LEFT JOIN acl_entries e
    ON ( e.class_id = o.class_id AND (e.object_identity_id = o.id OR e.object_identity_id IS NULL) ) 
LEFT JOIN acl_security_identities s ON ( s.id = e.security_identity_id )
WHERE (o.id =2189)

如何避免循环查询?

4

1 回答 1

0

Acl 身份可以在一个查询中预加载。看到这个问题:How do one use ACL to filter a list of domain-objects based on a certain user's permissions (eg EDIT)?

你将不得不向你的控制器添加这样的东西

// you entities
$companies = ...

// the acl provider
$aclProvider = $this->get('security.acl.provider');

// build up array of object identities
$oids = array();
foreach ($companies as $company) {
  $oids[] = ObjectIdentity::fromDomainObject($company);
}

// preload acls
$aclProvider->findAcls($oids); // preload Acls from database
于 2012-10-18T08:27:18.553 回答