Skip to content

Add an error message for non existing repository method & suggest to use CreatePaginatorTrait#1063

Merged
GSadee merged 1 commit into
Sylius:1.13from
loic425:improve-error-message-for-non-existing-repository-method
Sep 24, 2025
Merged

Add an error message for non existing repository method & suggest to use CreatePaginatorTrait#1063
GSadee merged 1 commit into
Sylius:1.13from
loic425:improve-error-message-for-non-existing-repository-method

Conversation

@loic425

@loic425 loic425 commented Sep 22, 2025

Copy link
Copy Markdown
Member
Q A
Bug fix? yes (better error message)
New feature?
BC breaks? no
Deprecations? no
Related tickets
License MIT

When using the "make:entity", a repository is created automatically, and normally it can be used directly with the new routing system without implementing the Sylius repository interface.
But here, in a collection operation, we define this createPaginator by default.

So here, first, the idea is to be more precise on what's happening when the default method and suggest using CreatePaginatorTrait.

That's another point, but we should also warn (or just fix it) about a non-grid object on the BootstrapAdminUi grid template.

Before
image

After
image

@loic425 loic425 changed the base branch from 1.13 to 1.14 September 22, 2025 07:43
@loic425 loic425 changed the base branch from 1.14 to 1.13 September 22, 2025 07:44
@loic425 loic425 force-pushed the improve-error-message-for-non-existing-repository-method branch from c81a20a to 6ebfa9a Compare September 22, 2025 07:45
@loic425 loic425 changed the base branch from 1.13 to 1.14 September 22, 2025 07:45
@loic425 loic425 force-pushed the improve-error-message-for-non-existing-repository-method branch 4 times, most recently from 68bc7fc to d49e43c Compare September 22, 2025 08:09
protected ?array $validationContext = null,
private ?array $normalizationContext = null,
private ?array $denormalizationContext = null,
private ?array $validationContext = null,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

already fixed on 1.14 branch.

Comment thread composer.json Outdated
@loic425 loic425 force-pushed the improve-error-message-for-non-existing-repository-method branch 5 times, most recently from b2d64c4 to bdc48c0 Compare September 22, 2025 09:59
@loic425 loic425 changed the base branch from 1.14 to 1.13 September 22, 2025 10:01
@loic425 loic425 force-pushed the improve-error-message-for-non-existing-repository-method branch from bdc48c0 to ede2d8c Compare September 22, 2025 13:22
@loic425 loic425 changed the title Add an error message for non existing repository method Add an error message for non existing repository method & suggest to use create paginator trait Sep 22, 2025
@loic425 loic425 changed the title Add an error message for non existing repository method & suggest to use create paginator trait Add an error message for non existing repository method & suggest to use CreatePaginatorTrait Sep 22, 2025
@loic425 loic425 force-pushed the improve-error-message-for-non-existing-repository-method branch 2 times, most recently from e16a725 to 3689132 Compare September 22, 2025 13:28
/**
* @param array $objects
*/
protected function getArrayPaginator($objects): Pagerfanta

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to avoid bc-breaks, I keep the missing argument type.

</thead>
<tbody>
{% for resource in resources.data %}
{% if not definition %}

@loic425 loic425 Sep 22, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sth that we could do on BootstrapAdminUi index template.

Comment on lines +44 to +46
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".');
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe move it to the top of ::createPaginator?
No point in applying criteria and sorting when it'll fail either way 🤷

@loic425 loic425 Sep 22, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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.

Comment on lines +23 to +27
/**
* @method ClassMetadata getClassMetadata()
* @method QueryBuilder createQueryBuilder(string $alias, string $indexBy = null)
*/
trait CreatePaginatorTrait

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about @mixin Doctrine\ORM\EntityRepository?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, indeed.

@loic425 loic425 force-pushed the improve-error-message-for-non-existing-repository-method branch from 3689132 to 37ae304 Compare September 22, 2025 14:21
use Doctrine\ORM\QueryBuilder;
use Pagerfanta\Adapter\ArrayAdapter;
use Pagerfanta\Doctrine\ORM\QueryAdapter;
use Pagerfanta\Pagerfanta;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we ready for

Pagerfanta\PagerfantaInterface

yet or it should be kept as it for BC?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to be ok so, let's go.
https://3v4l.org/O58qm


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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like a bigger change, is there some explanation why sylius/resource goes away from RepositoryInterface?

What will happen, if my custom repository doesn't implement any resource trait and interface?

@loic425 loic425 Sep 22, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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 is the reason why we have this issue (cf first error message on first screenshot).

This RepositoryInterface from Sylius extends that ObjectRepository from Doctrine.
But we do not need these three specific methods required by the Sylius one anymore (add, remove, createPaginator).

Comment thread src/Component/spec/Symfony/Request/State/ProviderSpec.php Outdated
$repositoryInstance = $this->locator->get($repository);

if (
!str_starts_with($method, 'find') && // magic calls on Doctrine repositories

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// magic calls on Doctrine repositories

That's not always true, I can have methods like findOneByAcme, which doesn't have any magic.

@loic425 loic425 Sep 22, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right, and not every "find" methods are magic.
We can replace the text with "To allow magic calls on Doctrine repository methods".

@loic425 loic425 force-pushed the improve-error-message-for-non-existing-repository-method branch 2 times, most recently from a0607da to dba5772 Compare September 24, 2025 11:40
@loic425 loic425 force-pushed the improve-error-message-for-non-existing-repository-method branch from dba5772 to 5f3251d Compare September 24, 2025 11:41
Comment thread psalm.xml

Copy link
Copy Markdown
Member

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 🤔

@GSadee GSadee merged commit f4d86d0 into Sylius:1.13 Sep 24, 2025
13 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants