具有多对多关系的行太多omines / datatables-bundle

时间:2019-02-14 13:02:27

标签: php symfony datatables

我正在使用bundle omines / datatables-bundle在我的Symfony应用程序中创建表。当我创建自定义查询时,左查询与具有“多对多”关系的字段联接在一起时,它将返回太多行。

我正在使用Symfony 4.2和omines / datatables-bundle捆绑包。最终结果是我发现的总结果数错误,并且表中有许多空白页。

这就是我的表格的创建方式

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');
                    }
                )
            )
        ;
    }
}

这是在我的实体类中定义的“多对多”关系。

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

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

以及吸气剂和吸气剂

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;
}

可能是什么原因?我该如何解决?

0 个答案:

没有答案