In doctrine.yaml, add a filter:
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
filters:
project_filter: App\Doctrine\AuthorizedUsersFilter
Then create a service in 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 '';
}