← Back
Editing: SolutionFilter.php
<?php namespace App\Solution\Filter; use App\Solution\Annotation\SolutionAware; use Doctrine\Common\Annotations\Reader; use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Query\Filter\SQLFilter; final class SolutionFilter extends SQLFilter { private ?Reader $reader = null; public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias): string { if (null === $this->reader) { throw new \RuntimeException(sprintf('An annotation reader must be provided. Be sure to call "%s::setAnnotationReader()".', __CLASS__)); } // The Doctrine filter is called for any query on any entity // Check if the current entity is "solution aware" (marked with an annotation) $solutionAware = $this->reader->getClassAnnotation($targetEntity->getReflectionClass(), SolutionAware::class); if (!$solutionAware) { return ''; } $fieldName = $solutionAware->solutionFieldName; try { // Don't worry, getParameter automatically escapes parameters $solutionId = $this->getParameter('id'); } catch (\InvalidArgumentException $e) { // No user id has been defined return ''; } if (empty($fieldName) || empty($solutionId)) { return ''; } return sprintf('%s.%s = %s', $targetTableAlias, $fieldName, $solutionId); } public function setAnnotationReader(Reader $reader): void { $this->reader = $reader; } }
Save File
Cancel