Sylius/SyliusResourceBundle

Resource naming with more than 2 dots

Prometee opened this issue ยท 3 comments

Sylius version affected: 1.11

Description

Using an alias with more than one dot.

\Sylius\Component\Resource\Metadata\Metadata::parseAlias will return an array with more than 2 elements and only the first two items of this array will be used as $applicationName and $name.
Then the metadata will search for the wrong entity reference leading to a wrong schema generation.

Steps to reproduce

sylius_resource:
    resources:
        app.quiz.question: # using 2 dots
            driver: doctrine/orm
            classes:
                model: App\Entity\Quiz\Question
                interface: App\Entity\QuestionInterface
            translation:
                classes:
                    model: App\Entity\Quiz\QuestionTranslation
                    interface: App\Entity\QuestionTranslationInterface

Will generate this diff :

CREATE TABLE app_response (id INT AUTO_INCREMENT NOT NULL, question_id INT NOT NULL, quiz_id INT DEFAULT NULL, INDEX IDX_A79809F81E27F6BF (question_id), INDEX IDX_A79809F8853CD175 (quiz_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB;

CREATE TABLE app_response_translation (id INT AUTO_INCREMENT NOT NULL, response VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB;

ALTER TABLE app_question_translation ADD CONSTRAINT FK_C27CB3C42C2AC5D3 FOREIGN KEY (translatable_id) REFERENCES app_question_translation (id) ON DELETE CASCADE;

We can see on the last SQL query a self referenced foreign key instead of a reference to the app_response.

Possible Solution

Replace the return of \Sylius\Component\Resource\Metadata\Metadata::parseAlias by :

return explode('.', $alias, 2);

to always return 2 array items.

What about using underscore instead of dots, and mentioning that in documentation?

I'm just a bit afraid that it will become messy if we allow multi dots here, especially for services names afterward.

The dots will be converted to underscores when generating service names.
It's already done like that for repositories:

[$applicationName, $resourceName] = explode('.', $alias, 2);

I had the same comment as @Roshyo, but if we already doing it, then let's just make it consistent ๐Ÿ‘