diff --git a/composer.json b/composer.json index b6b5ea66c..6ff5a7898 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,6 @@ "doctrine/collections": "^1.6 || ^2.0", "doctrine/common": "^3.1", "doctrine/persistence": "^3.0.2", - "sonata-project/doctrine-extensions": "^1.13 || ^2.0", "sonata-project/form-extensions": "^1.4 || ^2.0", "sonata-project/twig-extensions": "^1.3 || ^2.0", "symfony/config": "^5.4 || ^6.2", diff --git a/src/Entity/BaseUser3.php b/src/Entity/BaseUser3.php index 1969326b4..73caa18c1 100644 --- a/src/Entity/BaseUser3.php +++ b/src/Entity/BaseUser3.php @@ -13,18 +13,10 @@ namespace Sonata\UserBundle\Entity; -use Sonata\UserBundle\Model\User as AbstractedUser; - -class BaseUser3 extends AbstractedUser +/** + * This class is used to avoid error for users that have installed the version + * 5.12.0 of the SonataUserBundle and have not updated the User class. + */ +class BaseUser3 extends BaseUser { - public function prePersist(): void - { - $this->createdAt = new \DateTime(); - $this->updatedAt = new \DateTime(); - } - - public function preUpdate(): void - { - $this->updatedAt = new \DateTime(); - } } diff --git a/src/Listener/DoctrineMappingListener.php b/src/Listener/DoctrineMappingListener.php new file mode 100644 index 000000000..e094ffbb9 --- /dev/null +++ b/src/Listener/DoctrineMappingListener.php @@ -0,0 +1,54 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\UserBundle\Listener; + +use Doctrine\ORM\Event\LoadClassMetadataEventArgs; + +/** + * @internal + */ +final class DoctrineMappingListener +{ + private bool $isArrayTypeAvailable; + + public function __construct(private string $userClass) + { + $this->isArrayTypeAvailable = class_exists('Doctrine\DBAL\Types\ArrayType'); + } + + /** + * @throws \Doctrine\DBAL\Exception + */ + public function loadClassMetadata(LoadClassMetadataEventArgs $event): void + { + $metadata = $event->getClassMetadata(); + + if (!$this->isArrayTypeAvailable) { + return; + } + + if ($metadata->getName() !== $this->userClass) { + return; + } + + if (!$metadata->hasField('roles')) { + return; + } + + /** + * @psalm-suppress InvalidPropertyAssignmentValue + */ + $metadata->fieldMappings['roles']['type'] = 'array'; + } +} diff --git a/src/Resources/config/doctrine/BaseUser.orm.xml b/src/Resources/config/doctrine/BaseUser.orm.xml index 92ae3d230..11d5ec41f 100644 --- a/src/Resources/config/doctrine/BaseUser.orm.xml +++ b/src/Resources/config/doctrine/BaseUser.orm.xml @@ -11,7 +11,7 @@ - + diff --git a/src/Resources/config/doctrine/BaseUser3.orm.xml b/src/Resources/config/doctrine/BaseUser3.orm.xml deleted file mode 100644 index 6ebf323c6..000000000 --- a/src/Resources/config/doctrine/BaseUser3.orm.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/Resources/config/orm.php b/src/Resources/config/orm.php index ed65e6768..1c1c04983 100644 --- a/src/Resources/config/orm.php +++ b/src/Resources/config/orm.php @@ -14,6 +14,7 @@ namespace Symfony\Component\DependencyInjection\Loader\Configurator; use Sonata\UserBundle\Entity\UserManager; +use Sonata\UserBundle\Listener\DoctrineMappingListener; use Sonata\UserBundle\Listener\UserListener; return static function (ContainerConfigurator $containerConfigurator): void { @@ -37,5 +38,13 @@ ->args([ service('sonata.user.util.canonical_fields_updater'), service('sonata.user.manager.user'), + ]) + + ->set('sonata.user.doctrine.mapping_listener', DoctrineMappingListener::class) + ->tag('doctrine.event_listener', [ + 'event' => 'loadClassMetadata', + ]) + ->args([ + param('sonata.user.user.class'), ]); };