Querying deeply nested properties run an Invalid PathExpression. Must be a StateFieldPathExpression error

Viewed 27

I am trying to get all the distinct brands from my entity products in a school project using Symfony and Doctrine. After looking for a while I found a answer here and was able to create this function in my ProductRepository.

ProductRepository Method

public function getDistinct()
    {
        $query = $this->createQueryBuilder('product');
        $res = $query
            ->select ( "product.idProductModel")
            ->distinct(true)
            ->getQuery()
            ->getResult();
        return $res;
    }

However I have two problems. One: I run into an Invalid PathExpression. Must be a StateFieldPathExpression error when i try to do product.idProductModel or what i really want product.idProductModel.idBrand.name Both idProductModel and idBrand are foreign keys to my Product entity. This query works fine on non nested properties. I am able to get an array of objects of comments if i do for example product.comment. Which bring me to the second problem. I do not want an array of object but instead something that would look like this:

Format wanted

{
brands: [
    "Agilent",
    "Comtr"
    "Anot
      ],
types:[
     "Accelerometer",
     "Sonometer",
     "Micro-amplifier"
]
}

I also have the same problem if i try to do product.idProductModel.idType.name.

I am new to programming and would appreciate any help. Thank you.

0 Answers
Related