Skip to content

Add support using Route Entity for Entities with AutoIncrement Id #7859

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

Merged

Conversation

alexander-schranz
Copy link
Member

@alexander-schranz alexander-schranz commented Mar 25, 2025

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Fixed tickets fixes #
Related issues/PRs #7726
License MIT
Documentation PR sulu/sulu-docs#

What's in this PR?

We have some kind of a Chicken & Egg problem when try to use the Route entity with a Entity which using auto increment ids. As the Route entity requires the resourceId but the Entity itself does not yet have a getId().

In this pull request we are adding a new way to create a instance of Route without knowing the related resourceId at that time.

-       $route = new Route(
+       $route = Route::createRouteWithTempId(
            $localizedDimensionContent::getResourceKey(),
-           (string) $localizedDimensionContent->getResourceId(),
+           fn (): string => (string) $localizedDimensionContent->getResourceId(),
            $locale,
            $routeSlug,
        );

This new method will generate the route with a resourceId temp::<ulid::base58> which later will be replaced in a postFlush listener with the correct id of the entity. As discussed with @Prokyonn we decided to use ulid::base58 to avoid confusen with uuid, so if something went wrong and they see in the database temp::ulid they not confuse it with a uuid which may exist as page or article.

Why?

As the Route entity can be used also for entities which has a autoincremented ids which only exists after flush.

Example Usage

If you have a entity with autoincrement id use the createRouteWithTempId to create your route and the bundle will automatically on postFlush call your callback method to get the correct id and set it to the route entity correctly:

       $route = Route::createRouteWithTempId(
           'example',
           fn (): string => (string) $entity->getId(),
           'en',
           '/new-slug',
       );

       $entity->setRoute($route);

To Do

  • Create test for this
  • Create a documentation PR

@alexander-schranz alexander-schranz added Feature New functionality not yet included in Sulu DX Affecting the end developer labels Mar 25, 2025
@alexander-schranz alexander-schranz force-pushed the feature/route-for-autoincrement-entities branch 2 times, most recently from 7748228 to c45b289 Compare March 25, 2025 17:01
@alexander-schranz alexander-schranz force-pushed the feature/route-for-autoincrement-entities branch from c45b289 to d1e7d4f Compare March 25, 2025 17:09
@alexander-schranz alexander-schranz marked this pull request as ready for review March 25, 2025 17:20
@alexander-schranz alexander-schranz force-pushed the feature/route-for-autoincrement-entities branch from d1e7d4f to db052b7 Compare March 25, 2025 17:22
@Prokyonn Prokyonn merged commit bf95ebc into sulu:3.0 Mar 26, 2025
17 of 18 checks passed
@alexander-schranz alexander-schranz deleted the feature/route-for-autoincrement-entities branch March 26, 2025 09:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DX Affecting the end developer Feature New functionality not yet included in Sulu
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants