Last active
September 30, 2020 19:14
-
-
Save artyuum/98144d74f5a55a45cfa1544f6fb013bf to your computer and use it in GitHub Desktop.
Doctrine deleteBy() criteria
This file contains hidden or 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 | |
| class TokenRepository extends ServiceEntityRepository | |
| { | |
| public function __construct(ManagerRegistry $registry) | |
| { | |
| parent::__construct($registry, Token::class); | |
| } | |
| public function deleteBy(array $criteria) | |
| { | |
| $query = $this->createQueryBuilder('t')->delete(); | |
| foreach ($criteria as $property => $value) { | |
| $query | |
| ->andWhere('t.' . $property . ' = :' . $property) | |
| ->setParameter($property, $value) | |
| ; | |
| } | |
| return $query | |
| ->getQuery() | |
| ->execute() | |
| ; | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is how I implemented a findBy()-like method for the delete action in my token repository. It can be used in other repositories as well, you just need to change the alias used in the DQL.
Usage