Fetch the manyTo* entity collections of parts with other part columns

This reduces the query count and should improve performance especially for big tables.
This commit is contained in:
Jan Böhmer 2019-11-16 19:23:45 +01:00
parent 01b790a8d0
commit 988c53bead
2 changed files with 89 additions and 1 deletions

View file

@ -0,0 +1,81 @@
<?php
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
namespace App\DataTables\Adapter;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use Omines\DataTablesBundle\Adapter\AdapterQuery;
use Omines\DataTablesBundle\Adapter\Doctrine\ORMAdapter;
use Omines\DataTablesBundle\Column\AbstractColumn;
/**
* Override default ORM Adapter, to allow fetch joins (allow addSelect with ManyToOne Collections).
* This should improves performance for Part Tables.
* Based on: https://github.com/omines/datatables-bundle/blob/master/tests/Fixtures/AppBundle/DataTable/Adapter/CustomORMAdapter.php
* @package App\DataTables\Adapter
*/
class CustomORMAdapter extends ORMAdapter
{
protected $hydrationMode;
public function configure(array $options)
{
parent::configure($options);
$this->hydrationMode = isset($options['hydrate']) ? $options['hydrate'] : Query::HYDRATE_OBJECT;
}
protected function prepareQuery(AdapterQuery $query)
{
parent::prepareQuery($query);
$query->setIdentifierPropertyPath(null);
}
/**
* @param AdapterQuery $query
* @return \Traversable
*/
protected function getResults(AdapterQuery $query): \Traversable
{
/** @var QueryBuilder $builder */
$builder = $query->get('qb');
$state = $query->getState();
// Apply definitive view state for current 'page' of the table
foreach ($state->getOrderBy() as list($column, $direction)) {
/** @var AbstractColumn $column */
if ($column->isOrderable()) {
$builder->addOrderBy($column->getOrderField(), $direction);
}
}
if ($state->getLength() > 0) {
$builder
->setFirstResult($state->getStart())
->setMaxResults($state->getLength());
}
/*
* Use foreach instead of iterate to prevent group by from crashing
*/
foreach ($builder->getQuery()->getResult($this->hydrationMode) as $result) {
/*
* Return everything instead of first element
*/
yield $result;
}
}
}

View file

@ -21,6 +21,7 @@
namespace App\DataTables;
use App\DataTables\Adapter\CustomORMAdapter;
use App\DataTables\Column\EntityColumn;
use App\DataTables\Column\LocaleDateTimeColumn;
use App\DataTables\Column\MarkdownColumn;
@ -84,14 +85,20 @@ class PartsDataTable implements DataTableTypeInterface
->addSelect('partUnit')
->addSelect('master_picture_attachment')
->addSelect('footprint_attachment')
->addSelect('partLots')
->addSelect('orderdetails')
->addSelect('attachments')
->addSelect('storelocations')
->from(Part::class, 'part')
->leftJoin('part.category', 'category')
->leftJoin('part.master_picture_attachment', 'master_picture_attachment')
->leftJoin('part.partLots', 'partLots')
->leftJoin('partLots.storage_location', 'storelocations')
->leftJoin('part.footprint', 'footprint')
->leftJoin('footprint.master_picture_attachment', 'footprint_attachment')
->leftJoin('part.manufacturer', 'manufacturer')
->leftJoin('part.orderdetails', 'orderdetails')
->leftJoin('part.attachments', 'attachments')
->leftJoin('part.partUnit', 'partUnit');
}
@ -290,7 +297,7 @@ class PartsDataTable implements DataTableTypeInterface
])
->addOrderBy('name')
->createAdapter(ORMAdapter::class, [
->createAdapter(CustomORMAdapter::class, [
'query' => function (QueryBuilder $builder) {
$this->getQuery($builder);
},