Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to configure custom service for query builder in doctrine orm driver #54

Merged
merged 1 commit into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions src/Bundle/Doctrine/ORM/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

namespace Sylius\Bundle\GridBundle\Doctrine\ORM;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager;
use Sylius\Component\Grid\Data\DataSourceInterface;
use Sylius\Component\Grid\Data\DriverInterface;
use Sylius\Component\Grid\Parameters;
Expand All @@ -32,9 +32,6 @@ public function __construct(ManagerRegistry $managerRegistry)
$this->managerRegistry = $managerRegistry;
}

/**
* {@inheritdoc}
*/
public function getDataSource(array $configuration, Parameters $parameters): DataSourceInterface
{
if (!array_key_exists('class', $configuration)) {
Expand All @@ -47,15 +44,19 @@ public function getDataSource(array $configuration, Parameters $parameters): Dat
/** @var EntityRepository $repository */
$repository = $manager->getRepository($configuration['class']);

if (isset($configuration['repository']['method'])) {
$method = $configuration['repository']['method'];
$arguments = isset($configuration['repository']['arguments']) ? array_values($configuration['repository']['arguments']) : [];
if (!isset($configuration['repository']['method'])) {
return new DataSource($repository->createQueryBuilder('o'));
}

$arguments = isset($configuration['repository']['arguments']) ? array_values($configuration['repository']['arguments']) : [];
$method = $configuration['repository']['method'];
if (is_array($method) && 2 === count($method)) {
$queryBuilder = $method[0];
$method = $method[1];

$queryBuilder = $repository->$method(...$arguments);
} else {
$queryBuilder = $repository->createQueryBuilder('o');
return new DataSource($queryBuilder->$method(...$arguments));
}

return new DataSource($queryBuilder);
return new DataSource($repository->$method(...$arguments));
}
}
1 change: 1 addition & 0 deletions src/Bundle/test/app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ imports:
- { resource: "@SyliusGridBundle/test/app/config/parameters.yml" }
- { resource: "@SyliusGridBundle/test/app/config/resources.yml" }
- { resource: "@SyliusGridBundle/test/app/config/grids.yml" }
- { resource: "@SyliusGridBundle/test/app/config/services.yaml" }

framework:
assets: false
Expand Down
2 changes: 1 addition & 1 deletion src/Bundle/test/app/config/grids.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ sylius_grid:
options:
class: AppBundle\Entity\Book
repository:
method: createEnglishBooksQueryBuilder
method: [expr:service('app.english_books_query_builder'), create]
filters:
title:
type: string
Expand Down
6 changes: 6 additions & 0 deletions src/Bundle/test/app/config/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
app.english_books_query_builder:
class: AppBundle\QueryBuilder\EnglishBooksQueryBuilder
arguments:
- '@app.repository.book'
public: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace AppBundle\QueryBuilder;

use AppBundle\Entity\Author;
use AppBundle\Repository\BookRepository;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;

final class EnglishBooksQueryBuilder
{
/** @var BookRepository */
private $bookRepository;

public function __construct(BookRepository $bookRepository)
{
$this->bookRepository = $bookRepository;
}

public function create(): QueryBuilder
{
return $this->bookRepository->createQueryBuilder('b')
->innerJoin(Author::class, 'author', Join::WITH, 'author.id = b.author')
->innerJoin('author.nationality', 'na')
->andWhere('na.name = :nationality')
->setParameter(':nationality', 'English')
;
}
}
14 changes: 2 additions & 12 deletions src/Bundle/test/src/AppBundle/Repository/BookRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace AppBundle\Repository;

use AppBundle\Entity\Author;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
Expand All @@ -26,7 +25,8 @@ public function createListQueryBuilder(): QueryBuilder
->leftJoin('b.attributes', 'size', Join::WITH, 'size.code = :sizeCode')
->leftJoin('b.attributes', 'condition', Join::WITH, 'condition.code = :conditionCode')
->setParameter(':sizeCode', 'size')
->setParameter(':conditionCode', 'condition');
->setParameter(':conditionCode', 'condition')
;
}

public function createAmericanBooksQueryBuilder(): QueryBuilder
Expand All @@ -38,14 +38,4 @@ public function createAmericanBooksQueryBuilder(): QueryBuilder
->setParameter(':nationality', 'American')
;
}

public function createEnglishBooksQueryBuilder(): QueryBuilder
{
return $this->createQueryBuilder('b')
->innerJoin(Author::class, 'author', Join::WITH, 'author.id = b.author')
->innerJoin('author.nationality', 'na')
->andWhere('na.name = :nationality')
->setParameter(':nationality', 'English')
;
}
}