Skip to content
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

fix: use cascade 'merge' only if available #979

Merged
merged 2 commits into from
Nov 27, 2024

Conversation

fbuchlak
Copy link
Contributor

Closes #973.

@VincentLanglet Check is not performed on EntityManagerInterface because there is no merge method declared in supported doctrine/orm versions. Previously it was inherited from ObjectManager in doctrine/persistence, but it was removed in persistence 3.0.

Changelog

### Fixed
- Use cascade `merge` only if it's available in `doctrine/orm`

@@ -133,11 +134,15 @@ private function registerSonataDoctrineMapping(array $config): void
->addOrder('position', 'ASC')
);

$categoryCascade = ['persist', 'refresh', 'detach'];
if (class_exists(EntityManager::class) && method_exists(EntityManager::class, 'merge')) { // @phpstan-ignore-line
Copy link
Member

Choose a reason for hiding this comment

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

You will lose the merge for ODM user.
https://github.com/doctrine/mongodb-odm/blob/2.10.x/lib/Doctrine/ODM/MongoDB/DocumentManager.php

it should be

if (
    class_exists(EntityManager::class) && method_exists(EntityManager::class, 'merge')
    || class_exists(DocumentManager::class) && method_exists(DocumentManager::class, 'merge')
)

but I assume it will create issue when both lib are installed.

So we have to know which entity we extends.

Maybe

$classParents = class_parents($config['class']['category']);

if (\in_array(Entity/BaseCategory::class, $classParents, true) && class_exists(EntityManager::class) && !method_exists(EntityManager::class, 'merge')) {
     unset($categoryCascade['merge']);
}

?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for late reply. I've checked odm src and it seems like merge isn't deprecated (even in 3.0 branch), so 2nd option should be just fine.

@VincentLanglet VincentLanglet merged commit cb132b9 into sonata-project:4.x Nov 27, 2024
24 checks passed
@VincentLanglet
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid cascade mapping for doctrine/orm 3
2 participants