From f3382cb0df1ee00d00d739c643f5bfc2ad488669 Mon Sep 17 00:00:00 2001 From: Romain Geissler Date: Sun, 22 Apr 2012 17:51:06 +0200 Subject: [PATCH] Fix multiple entity join filtering. --- Builder/DatagridBuilder.php | 4 ++-- Datagrid/ProxyQuery.php | 21 +++++++++++++++++++++ Filter/Filter.php | 10 +--------- Filter/ModelFilter.php | 8 ++------ 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/Builder/DatagridBuilder.php b/Builder/DatagridBuilder.php index 0bd192f1b..0003dd028 100644 --- a/Builder/DatagridBuilder.php +++ b/Builder/DatagridBuilder.php @@ -60,12 +60,12 @@ public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInter // set the default field mapping if (isset($metadata->fieldMappings[$lastPropertyName])) { - $fieldDescription->setFieldMapping($metadata->fieldMappings[$lastPropertyName]); + $fieldDescription->setOption('field_mapping', $fieldDescription->getOption('field_mapping', $metadata->fieldMappings[$lastPropertyName])); } // set the default association mapping if (isset($metadata->associationMappings[$lastPropertyName])) { - $fieldDescription->setAssociationMapping($metadata->associationMappings[$lastPropertyName]); + $fieldDescription->setOption('association_mapping', $fieldDescription->getOption('association_mapping', $metadata->associationMappings[$lastPropertyName])); } $fieldDescription->setOption('parent_association_mappings', $fieldDescription->getOption('parent_association_mappings', $parentAssociationMappings)); diff --git a/Datagrid/ProxyQuery.php b/Datagrid/ProxyQuery.php index d9ab8f562..a0b40d281 100644 --- a/Datagrid/ProxyQuery.php +++ b/Datagrid/ProxyQuery.php @@ -27,10 +27,13 @@ class ProxyQuery implements ProxyQueryInterface protected $parameterUniqueId; + protected $entityJoinAliases; + public function __construct(QueryBuilder $queryBuilder) { $this->queryBuilder = $queryBuilder; $this->uniqueParameterId = 0; + $this->entityJoinAliases = array(); } public function execute(array $params = array(), $hydrationMode = null) @@ -171,4 +174,22 @@ public function getUniqueParameterId() { return $this->uniqueParameterId++; } + + public function entityJoin($associationMappings) + { + $alias = $this->queryBuilder->getRootAlias(); + $newAlias = 's'; + + foreach($associationMappings as $associationMapping){ + $newAlias .= '_'.$associationMapping['fieldName']; + if (!in_array($newAlias, $this->entityJoinAliases)) { + $this->entityJoinAliases[] = $newAlias; + $this->queryBuilder->leftJoin(sprintf('%s.%s', $alias, $associationMapping['fieldName']), $newAlias); + } + + $alias = $newAlias; + } + + return $alias; + } } diff --git a/Filter/Filter.php b/Filter/Filter.php index 5eb27f97f..1b4004ea1 100644 --- a/Filter/Filter.php +++ b/Filter/Filter.php @@ -28,15 +28,7 @@ public function apply($queryBuilder, $value) protected function association($queryBuilder, $value) { - $parentAssociationMappings = $this->getParentAssociationMappings(); - $alias = $this->getOption('alias', $queryBuilder->getRootAlias()); - $newAlias = 's_'.$alias; - - foreach($parentAssociationMappings as $parentAssociationMapping){ - $newAlias .= '_'.$parentAssociationMapping['fieldName']; - $queryBuilder->leftJoin(sprintf('%s.%s', $alias, $parentAssociationMapping['fieldName']), $newAlias); - $alias = $newAlias; - } + $alias = $queryBuilder->entityJoin($this->getParentAssociationMappings()); return array($alias, $this->getFieldName()); } diff --git a/Filter/ModelFilter.php b/Filter/ModelFilter.php index afddc192e..daa2a9503 100644 --- a/Filter/ModelFilter.php +++ b/Filter/ModelFilter.php @@ -73,8 +73,6 @@ protected function handleModel($queryBuilder, $alias, $field, $data) protected function association($queryBuilder, $data) { - list($alias, $field) = parent::association($queryBuilder, $data); - $types = array( ClassMetadataInfo::ONE_TO_ONE, ClassMetadataInfo::ONE_TO_MANY, @@ -86,11 +84,9 @@ protected function association($queryBuilder, $data) throw new \RunTimeException('Invalid mapping type'); } - $newAlias = $alias.'_'.$field; - - $queryBuilder->leftJoin(sprintf('%s.%s', $alias, $field), $newAlias); + $alias = $queryBuilder->entityJoin($this->getParentAssociationMappings() + array($this->getAssociationMapping())); - return array($newAlias, false); + return array($alias, false); } public function getDefaultOptions()