omines / datatables-bundle

DataTables bundle for Symfony
https://omines.github.io/datatables-bundle/
MIT License
258 stars 115 forks source link

Too many rows with Many To Many Relationship #68

Closed graferowy closed 5 years ago

graferowy commented 5 years ago

Hi!

Whenever I try to leftJoin with a field that has ManyToMany relationship, 'query' in my ORMAdapter returns too many results. The effect is that I have wrong number of total results and many blank pages in my table. What might be the cause of it? And how can I fix it?

class TestTableType implements DataTableTypeInterface
{
    public function configure(DataTable $dataTable, array $options)
    {
        $dataTable
            ->add('id',
                NumberColumn::class,
                array(
                    'label'            => 'ID',
                    'globalSearchable' => false
                )
            )
            ->createAdapter(ORMAdapter::class,
                array(
                    'entity' => Test::class,
                    'query'  => function (QueryBuilder $builder)
                    {
                        $builder
                            ->distinct()
                            ->select('t')
                            ->from(Test::class, 't')
                            ->leftJoin('t.products', 'prod');
                    }
                )
            )
        ;
    }
}

The field in my entity looks like this

**
     * @ORM\ManyToMany(targetEntity="App\Entity\Product", inversedBy="terms")
     * @ORM\JoinTable(name="product_has_test")
     */
    private $products;

public function __construct()
    {
        $this->products = new \Doctrine\Common\Collections\ArrayCollection();
    }

And the getters and setters look like this

public function addProduct(\App\Entity\Product $product)
    {
        $this->products[] = $product;

        return $this;
    }
public function removeProduct(\App\Entity\Product $product)
    {
        $this->products->removeElement($product);
    }
public function getProducts()
    {
        return $this->products;
    }
curry684 commented 5 years ago

Since it is unlikely this has anything to do with our bundle I'm closing this as offtopic for the issues list of the table. Please read the support section of the README.