Skip to content

Instantly share code, notes, and snippets.

@ker0x
Last active October 10, 2022 16:01
Show Gist options
  • Save ker0x/661d4a2e29347dca594fba8eccc23189 to your computer and use it in GitHub Desktop.
Save ker0x/661d4a2e29347dca594fba8eccc23189 to your computer and use it in GitHub Desktop.
Symfony User findByRole
<?php
public function findByRole(Role $role): mixed
{
$rsm = $this->createResultSetMappingBuilder('u');
$query = <<<SQL
SELECT %s
FROM user u
WHERE u.roles::jsonb ?? :role
SQL;
$rawQuery = sprintf($query, $rsm->generateSelectClause());
$query = $this->getEntityManager()->createNativeQuery($rawQuery, $rsm);
$query->setParameter('role', $role->getValue());
return $query->getResult();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment