Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/Doctrine/Filter/AccountFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,35 @@
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Query\Filter\SQLFilter;
use Softspring\AccountBundle\Model\AccountFilterInterface;
use Softspring\AccountBundle\Model\AccountInterface;

class AccountFilter extends SQLFilter
{
// values for this request
public static string $FIELD_NAME = 'account_id';
public static string $PARAMETER_NAME = '_account';

public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias): string
{
if (!$targetEntity->reflClass->implementsInterface(AccountFilterInterface::class)) {
return '';
}

// TODO dynamic account field and parameter name
return $targetTableAlias.'.account_id = '.$this->getParameter('_account');
$value = $this->getParameter(self::$PARAMETER_NAME);

$fieldName = self::$FIELD_NAME;

// search for the first association mapping that implements AccountInterface
foreach ($targetEntity->getAssociationMappings() as $mapping) {
$targetEntityRef = new \ReflectionClass($mapping->targetEntity);
if ($targetEntityRef->implementsInterface(AccountInterface::class)) {
if (sizeof($mapping->joinColumnFieldNames) === 1) {
$fieldName = current($mapping->joinColumnFieldNames);
}
break;
}
}

return "$targetTableAlias.$fieldName = $value";
}
}
Loading