Search code examples
symfonyfilterdoctrinearraycollection

How can I filter doctrine query by related id (Symfony 4)?


In my entity:

  /**
  * @ORM\ManyToMany(targetEntity="Productgroup", inversedBy="fields")
  * @ORM\JoinColumn(name="productgroup", referencedColumnName="id")
  */
  private $productgroup;

  public function getProductgroup()
  {
    return $this->productgroup;
  }

  public function setProductgroup($productgroup): self
  {
    $this->productgroup = $productgroup;

    return $this;
  }

  public function __construct()
  {
    $this->productgroup = new ArrayCollection();
  }

In my Controller:

$group = $this->getDoctrine()->getRepository(class::fields)->findAll();

The output:

array:2 [▼
  0 => Fields {#120842 ▼
    -id: 3
    -name: "cat"
    -unique_id: "5a38c820ed"
    -productgroup: PersistentCollection {#120846 ▼
      -snapshot: array:1 [ …1]
      -owner: Fields {#120842}
      -association: array:20 [ …20]
      -em: EntityManager {#114768 …11}
      -backRefFieldName: "fields"
      -typeClass: ClassMetadata {#119877 …}
      -isDirty: false
      #collection: ArrayCollection {#120847 ▼
        -elements: array:1 [▼
          0 => Productgroup {#120528 ▼
            -id: 6
            -name: "Animals"
            -unique_id: "9e4ef1c46f"
            -fields: PersistentCollection {#120739 ▶}
          }
        ]
      }
      #initialized: true
    }
    -type: Type {#120923 ▶}
  }
  1 => Fields {#120924 ▼
    -id: 5
    -name: "horse"
    -unique_id: "c3890b9287"
    -productgroup: PersistentCollection {#120925 ▼
      -snapshot: []
      -owner: Fields {#120924}
      -association: array:20 [ …20]
      -em: EntityManager {#114768 …11}
      -backRefFieldName: "fields"
      -typeClass: ClassMetadata {#119877 …}
      -isDirty: false
      #collection: ArrayCollection {#120926 ▼
        -elements: []
      }
      #initialized: false
    }
    -type: Type {#120927 ▶}
  }
]

I want to filter now all $group to output only the fields that are not connected to the productgroup with the id 6. My approach:

In my controller:

$group = $this->getDoctrine()->getRepository(class:fields)->filterByColletion(6);

In my Repository:

 public function filterByColletion($id)
    {
      return $this->createQueryBuilder('p')
      ->addSelect('f')
      ->from('App\Entity\Fields', 'f')
      ->leftJoin('f.productgroup', 'productgroup')
      ->andWhere('f.productgroup != 6')
      ->getQuery()
      ->execute();
    }

The error is

Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected

As a result I expect only group contains horse.


Solution

  • You have ->andWhere('f.productgroup != 6') in your query, but f.productgroup is not a plain value field, but relation. You need to apply your condition to value field, so it would be something like this:

    ->leftJoin('f.productgroup', 'pg')
    ->andWhere('pg.id != 6')
    

    I've used pg.id in this example, but you need to use name of actual value field name from Productgroup entity that you want to apply condition to.

    Side note: it is better to not embed value directly into query, but pass it as parameter instead:

    ->leftJoin('f.productgroup', 'pg')
    ->andWhere('pg.id != :id')
    ->setParameter(':id', 6)