FORSMILE
JA
Symfony2021/05/10

Iterate with fetch join in class ~ not allowed. Error Fix [Symfony]

This blog post serves as a memorandum of the solution I found for an error that occurred while developing a net shop system called EC-CUBE, which is based on Symfony.

Back to Blog

This blog post serves as a memorandum of the solution I found for an error that occurred while developing a net shop system called EC-CUBE, which is based on Symfony.

I had to execute a somewhat heavy SQL query, so when I tried to process it using the ORM's `iterate` method, the following error occurred.

text
Iterate with fetch join in class Eccube\Entity\ProductClass using association Product not allowed.

→Iterate with fetch join in subquery

ITERATE WITH FETCH JOIN IN CLASS ~ NOT ALLOWED. ERROR RESOLUTION

To summarize the Git message, it suggested using `distinct`.

The issue was resolved by applying `distinct` to the select query.

By the way, `distinct` is a command that consolidates duplicate records into a single one.

Solved Code

php
$qb->select('p, pc')
    ->distinct();

$result = $qb->iterate();

…

Cause

This error seems to occur when an Entity that is being retrieved has a join within it.

The actual Entity section is shown below (EC-CUBE src).

javascript
/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\OneToMany(targetEntity="Eccube\Entity\ProductClass", mappedBy="Product", cascade={"persist","remove"})
 */
private $ProductClasses;

This is an example where `ProductClass` is joined within `Entity/Product.php`. As shown above, you need to apply `distinct()` when a join is present.

📦
Amazon で関連書籍・ツールを検索
Symfony PHP framework book
Amazonで探す →(アソシエイトリンク)