Skip to content
Open
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,20 @@ foreach ($profiles as $_profile) {

You should always use new `EntityManager` instance, not the default one (because of `EntityManager::clear`).

### Default collections order by `id`

If you want all collection associations (`OneToMany`, `ManyToMany`) to be automatically ordered by the primary key `id` (ASC), you can use the built-in Doctrine subscriber `DefaultOrderByIdSubscriber`.
It hooks into the metadata loading process and applies `ORDER BY id ASC` to every collection that does not already define its own ordering.
This ensures stable and deterministic ordering across your application without having to manually specify `orderBy` in every entity.

Simply enable the subscriber in your neon configuration:

```yaml
services:
- ADT\DoctrineComponents\Subscribers\DefaultOrderByIdSubscriber
```


## Tips

- Always have all logic inside a `filter` or `order` callback. This will ensure that all dependencies (like a logged user etc.) are already set.
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"require": {
"php": ">=8.4",
"doctrine/orm": "^2.18|^3.0",
"doctrine/event-manager": "^1.0|^2.0",
"nette/utils": "^3.2|^4.0",
"tracy/tracy": "^2.10",
"psr/log": "^3.0",
Expand Down
51 changes: 51 additions & 0 deletions src/Subscribers/DefaultOrderByIdSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace ADT\DoctrineComponents\Subscribers;

use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ClassMetadata;

/**
* Doctrine event subscriber that ensures all collection associations
* (OneToMany, ManyToMany) have a stable default ordering by "id" (ASC),
* unless an explicit order for "id" is already defined on the association.
*/
final class DefaultOrderByIdSubscriber implements EventSubscriber
{
public function getSubscribedEvents(): array
{
return [
Events::loadClassMetadata,
];
}

public function loadClassMetadata(LoadClassMetadataEventArgs $args): void
{
$metadata = $args->getClassMetadata();

foreach ($metadata->associationMappings as $fieldName => $mapping) {
if (!in_array($mapping['type'], [ClassMetadata::ONE_TO_MANY, ClassMetadata::MANY_TO_MANY], true)) {
continue;
}

// Add "ORDER BY id ASC", except there is "ORDER BY id ASC/DESC"
$addOrderById = true;
$orderBy = $mapping['orderBy'] ?? [];
foreach ($orderBy as $sort => $order) {
if ($sort === 'id') {
$addOrderById = false;
break;
}
}

if ($addOrderById) {
$orderBy['id'] = 'ASC';
$metadata->associationMappings[$fieldName]['orderBy'] = $orderBy;
}
}
}
}