Hello,

I'm writing a class that's able to add filters to a given query builder.

I'm having a problem when I want to add a filter in a field that's not a 
part of the root class of the query, because I don't know the alias name of 
the related class.

I've used query builder getDQLPart function to retrieve all query joins. 
This returns an array where I get the join alias, and the relation name, 
but it doesn't tell me the name of the related class.

I think it's possible to get the name of the related class by reading 
Doctrine model information but I couldn't find a way to do this properly. 

The way I found is to rewrite a function that's in the 
Doctrine\ORM\EntityRepository class : getClassMetadata()

This function is declared as protected, If I modify it to make it public, 
I'm able to achieve what I'm looking for. 

Of course, this is not a good solution, and my question is if there's 
another way to implement this functionnality. 

This is the code I'm using to write my function

    public static function getQueryAlias($qb, $attdef, $repository){
        
        $relationName = '';
        $classmetadata = $repository->getClassMetadata();
        foreach ($classmetadata->getAssociationMappings() as $key => 
$relation){
            //$attdef is an array with fields definitions I use to set up 
filters. $attdef['name'] matchs doctrine fieldname
            if($relation['fieldName'] == $attdef['name']){
                $relationName = $key;
            }
        }
        
        if($relationName != ''){
            foreach ($qb->getDQLPart('join') as $joins){
                foreach($joins as $join){
                    $arr = explode('.', $join->getJoin( ));
                    if($arr[1] == $relationName){
                        return $join->getAlias();
                    }
                }
            
            }
        }
        return false;
    }  

Thanks for your answers

Andrés

-- 
You received this message because you are subscribed to the Google Groups 
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to