我正在使用Symfony2,Doctrine和ACL,我遇到了性能问题。
在树枝模板中,我需要检查集合中每个公司的当前用户角色以显示链接:
{%- 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)
如何避免在循环中查询?
ACL身份可以在一个查询中预加载。请参阅此问题:如何使用ACL根据某个用户的权限(例如编辑)过滤域列表?
您必须向控制器添加类似的东西
// 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