-
-
Notifications
You must be signed in to change notification settings - Fork 166
Add an error message for non existing repository method & suggest to use CreatePaginatorTrait #1063
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Sylius package. | ||
| * | ||
| * (c) Sylius Sp. z o.o. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sylius\Bundle\ResourceBundle\Doctrine\ORM; | ||
|
|
||
| use Doctrine\ORM\EntityRepository as DoctrineEntityRepository; | ||
| use Doctrine\ORM\QueryBuilder; | ||
| use Pagerfanta\Adapter\ArrayAdapter; | ||
| use Pagerfanta\Doctrine\ORM\QueryAdapter; | ||
| use Pagerfanta\Pagerfanta; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we ready for Pagerfanta\PagerfantaInterfaceyet or it should be kept as it for BC?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems to be ok so, let's go. |
||
| use Pagerfanta\PagerfantaInterface; | ||
| use Sylius\Resource\Model\ResourceInterface; | ||
|
|
||
| /** | ||
| * @mixin DoctrineEntityRepository | ||
| */ | ||
| trait CreatePaginatorTrait | ||
|
Comment on lines
+24
to
+27
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done, indeed. |
||
| { | ||
| /** | ||
| * @return iterable<int, ResourceInterface> | ||
| */ | ||
| public function createPaginator(array $criteria = [], array $sorting = []): iterable | ||
| { | ||
| $queryBuilder = $this->createQueryBuilder('o'); | ||
|
|
||
| $this->applyCriteria($queryBuilder, $criteria); | ||
| $this->applySorting($queryBuilder, $sorting); | ||
|
|
||
| return $this->getPaginator($queryBuilder); | ||
| } | ||
|
|
||
| protected function getPaginator(QueryBuilder $queryBuilder): PagerfantaInterface | ||
| { | ||
| if (!class_exists(QueryAdapter::class)) { | ||
| throw new \LogicException('You can not use the "paginator" if Pargefanta Doctrine ORM Adapter is not available. Try running "composer require pagerfanta/doctrine-orm-adapter".'); | ||
| } | ||
|
Comment on lines
+44
to
+46
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe move it to the top of
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's right, but maybe a user can call the getPaginator without use createPaginator method, cause this method is protected. |
||
|
|
||
| // Use output walkers option in the query adapter should be false as it affects performance greatly (see sylius/sylius#3775) | ||
| return new Pagerfanta(new QueryAdapter($queryBuilder, false, false)); | ||
| } | ||
|
|
||
| /** | ||
| * @param array $objects | ||
| */ | ||
| protected function getArrayPaginator($objects): PagerfantaInterface | ||
| { | ||
| return new Pagerfanta(new ArrayAdapter($objects)); | ||
| } | ||
|
|
||
| protected function applyCriteria(QueryBuilder $queryBuilder, array $criteria = []): void | ||
| { | ||
| foreach ($criteria as $property => $value) { | ||
| if (!in_array($property, array_merge($this->getClassMetadata()->getAssociationNames(), $this->getClassMetadata()->getFieldNames()), true)) { | ||
| continue; | ||
| } | ||
|
|
||
| $name = $this->getPropertyName($property); | ||
|
|
||
| if (null === $value) { | ||
| $queryBuilder->andWhere($queryBuilder->expr()->isNull($name)); | ||
| } elseif (is_array($value)) { | ||
| $queryBuilder->andWhere($queryBuilder->expr()->in($name, $value)); | ||
| } elseif ('' !== $value) { | ||
| $parameter = str_replace('.', '_', $property); | ||
| $queryBuilder | ||
| ->andWhere($queryBuilder->expr()->eq($name, ':' . $parameter)) | ||
| ->setParameter($parameter, $value) | ||
| ; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| protected function applySorting(QueryBuilder $queryBuilder, array $sorting = []): void | ||
| { | ||
| foreach ($sorting as $property => $order) { | ||
| if (!in_array($property, array_merge($this->getClassMetadata()->getAssociationNames(), $this->getClassMetadata()->getFieldNames()), true)) { | ||
| continue; | ||
| } | ||
|
|
||
| if (!empty($order)) { | ||
| $queryBuilder->addOrderBy($this->getPropertyName($property), $order); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| protected function getPropertyName(string $name): string | ||
| { | ||
| if (!str_contains($name, '.')) { | ||
| return 'o' . '.' . $name; | ||
| } | ||
|
|
||
| return $name; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,7 +69,7 @@ | |
| <service id="Sylius\Bundle\ResourceBundle\Form\Type\DefaultResourceType" alias="sylius.form.type.default" /> | ||
|
|
||
| <service id="sylius.registry.resource_repository" class="Sylius\Component\Registry\ServiceRegistry" public="false"> | ||
| <argument>Sylius\Component\Resource\Repository\RepositoryInterface</argument> | ||
| <argument>Doctrine\Persistence\ObjectRepository</argument> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels like a bigger change, is there some explanation why What will happen, if my custom repository doesn't implement any resource trait and interface?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, the reason is that we do not need to implement that specific interface anymore in the new routing system. This RepositoryInterface from Sylius extends that ObjectRepository from Doctrine. |
||
| <argument>resource repository</argument> | ||
| </service> | ||
| <service id="sylius.registry.form_builder" class="Sylius\Component\Registry\ServiceRegistry" public="false"> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Sylius package. | ||
| * | ||
| * (c) Sylius Sp. z o.o. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Subscription\Entity; | ||
|
|
||
| use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; | ||
| use Doctrine\Persistence\ManagerRegistry; | ||
| use Sylius\Bundle\ResourceBundle\Doctrine\ORM\CreatePaginatorTrait; | ||
|
|
||
| final class SubscriptionRepository extends ServiceEntityRepository | ||
| { | ||
| use CreatePaginatorTrait; | ||
|
|
||
| public function __construct( | ||
| public readonly ManagerRegistry $registry, | ||
| ) { | ||
| parent::__construct($this->registry, Subscription::class); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably get rid of psalm in favour of phpstan, as we did in other repositories 🤔