我有一个包含数十万条记录的数据库。出于商业原因,只有从事特定项目的用户才能查看某些记录,我们通过project_code字段识别这些记录。
我认为在学说中有一种根据用户角色过滤记录的工具。
谁能向我解释一下我如何使用这种行级过滤。
在doctrine.yaml中,添加一个过滤器:
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
filters:
project_filter: AppDoctrineAuthorizedUsersFilter
然后在 src/Doctrine/AuthorizedUsersFilter 中创建一个服务.php
class AuthorizedUsersFilter extends SQLFilter
{
/**
* @return string The constraint SQL if there is available, empty string otherwise.
*/
public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias): string
{
// check if this class is one that will be filtered, e.g. this user can only view records with a certain project ID
if (/* a class that is filtered */) {
return sprintf('%s.project_id = %s', $targetTableAlias, $project_id);
}
return '';
}