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

Multiple Sulu entity inheritance #7385

Open
bober888 opened this issue Apr 30, 2024 · 2 comments
Open

Multiple Sulu entity inheritance #7385

bober888 opened this issue Apr 30, 2024 · 2 comments
Labels
Bug Error or unexpected behavior of already existing functionality

Comments

@bober888
Copy link

Q A
Sulu Version 2.5.10
PHP Version 8.1
DB Version PostgreSQL 10.12
Browser Version --

Actual Behavior + Steps to Reproduce

The problem occurs when overloading entities multiple times. For example, we create a new entity Base\User, which inherits from Sulu\User. We override the email column in the .orm mapping using XML with the <attribute-override> attribute. Then we create another entity App\User, which inherits from Base/User:

<?php

declare(strict_types=1);

namespace App;

class User extends \Base\User
{
}
<?php

declare(strict_types=1);

namespace Base;

class User extends \Sulu\Bundle\SecurityBundle\Entity\User
{
}

Base\User mapping:

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
    <mapped-superclass name="Base\User" table="se_users">
            <attribute-overrides>
                <attribute-override name="email">
                    <field unique="false" length="255" nullable="true"/>
                </attribute-override>
             <attribute-overrides>
    </mapped-superclass>
</doctrine-mapping>

Upon running doctrine:scheme:validate, we encounter the error "Invalid field override named 'email' for class 'Base\User'". The issue arises in the class Sulu\Component\Persistence\EventSubscriber\ORM\MetadataSubscriber, which takes parents (Base\User then Sulu\User) from App\User and sets metadata for App\User associations. The error occurs at line 105: $configuration->getMetadataDriverImpl()->loadMetadataForClass($parent, $parentMetadata);, during the initialization of metadata for Base\User (in the loadMetadataForClass method) because it does not consider that the entity may have its own parents.

Expected Behavior + Possible Solutions

The expected behavior is that the method should initialize metadata for all parent entities and gradually set it for all children. A correct implementation can be found, for example, in the Doctrine\Persistence\Mapping\AbstractClassMetadataFactory class in the getAllMetadata() method.

@bober888 bober888 added the Bug Error or unexpected behavior of already existing functionality label Apr 30, 2024
@bober888
Copy link
Author

As possible solution:
subcriber.patch

Patched on one project, where this problem was detected. With this solution works fine

@alexander-schranz
Copy link
Member

alexander-schranz commented May 3, 2024

We don't have yet any experience with using a mapped super class between of them or using attribute-override, but think it should be possible with some refactoring. I remeber that we used a normaly entity in between with them adding new properties where there no problem.

Still I want to mention that we can never guarante backwards compatibility for models which are changed via attribute-override, this may can crash if Sulu depends on some cases like this attribute being unique or a specific length for an index.

If you want to contribute a patch, give it a try, but it will require some special test setup so we can guarantee it will work in future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Error or unexpected behavior of already existing functionality
Projects
None yet
Development

No branches or pull requests

2 participants