我正在使用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)
如何避免在循环中查询?
答案 0 :(得分:0)
可以在一个查询中预加载Acl身份。请参阅此问题:How do one use ACL to filter a list of domain-objects according to a certain user's permissions (e.g. 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