Skip to content

Commit

Permalink
extracted Type operations into getFieldCustomType, extracted loop-par…
Browse files Browse the repository at this point in the history
…t from addIdentifiersToQuery to buildInnerIdentifier
  • Loading branch information
oleg-andreyev committed Mar 29, 2020
1 parent edee419 commit 116056c
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/Model/ModelManager.php
Expand Up @@ -341,7 +341,7 @@ public function getIdentifierValues($entity)
}

$fieldType = $metadata->getTypeOfField($name);
$type = $fieldType && Type::hasType($fieldType) ? Type::getType($fieldType) : null;
$type = $this->getFieldCustomType($fieldType);
if ($type) {
$identifiers[] = $this->getValueFromType($value, $type, $fieldType, $platform);

Expand Down Expand Up @@ -404,31 +404,23 @@ public function addIdentifiersToQuery($class, ProxyQueryInterface $queryProxy, a
{
$fieldNames = $this->getIdentifierFieldNames($class);
$qb = $queryProxy->getQueryBuilder();
/** @var ClassMetadata $classMetadata */
$classMetadata = $this->getMetadata($class);

$prefix = $this->generatePrefix();
$sqls = [];
$ors = [];
foreach ($idx as $pos => $id) {
$ids = explode(self::ID_SEPARATOR, $id);

$ands = [];
foreach ($fieldNames as $posName => $name) {
$parameterName = sprintf('field_%s_%s_%d', $prefix, $name, $pos);
$ands[] = sprintf('%s.%s = :%s', current($qb->getRootAliases()), $name, $parameterName);

$fieldMapping = $classMetadata->getFieldMapping($name);
$type = null;
if (Type::hasType($fieldMapping['type'])) {
$type = Type::getType($fieldMapping['type']);
}

$qb->setParameter($parameterName, $ids[$posName], $type ? $type->getName() : null);
foreach ($fieldNames as $posName => $field) {
$ands[] = $this->buildInnerIdentifier($prefix, $field, $ids[$posName], $pos, $qb, $classMetadata);
}

$sqls[] = implode(' AND ', $ands);
$ors[] = implode(' AND ', $ands);
}

$qb->andWhere(sprintf('( %s )', implode(' OR ', $sqls)));
$qb->andWhere(sprintf('( %s )', implode(' OR ', $ors)));
}

public function batchDelete($class, ProxyQueryInterface $queryProxy)
Expand Down Expand Up @@ -679,4 +671,20 @@ private function getValueFromType($value, Type $type, string $fieldType, Abstrac

return (string) $type->convertToDatabaseValue($value, $platform);
}

private function getFieldCustomType(?string $fieldType): ?Type
{
return Type::hasType($fieldType) ? Type::getType($fieldType) : null;
}

private function buildInnerIdentifier(string $prefix, string $field, $value, int $pos, QueryBuilder $qb, ClassMetadata $classMetadata)
{
$parameterName = sprintf('field_%s_%s_%d', $prefix, $field, $pos);

$fieldMapping = $classMetadata->getFieldMapping($field);
$type = $this->getFieldCustomType($fieldMapping['type']);
$qb->setParameter($parameterName, $value, $type ? $type->getName() : null);

return sprintf('%s.%s = :%s', current($qb->getRootAliases()), $field, $parameterName);
}
}

0 comments on commit 116056c

Please sign in to comment.