Last active
October 10, 2022 16:01
-
-
Save ker0x/661d4a2e29347dca594fba8eccc23189 to your computer and use it in GitHub Desktop.
Symfony User findByRole
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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