Skip to content

Commit

Permalink
Make the code compatible with DBAL 3 and 4 by swithing the type when …
Browse files Browse the repository at this point in the history
…the Mapping is loaded
  • Loading branch information
cjtrbx committed May 15, 2024
1 parent a6169f3 commit 85202a9
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 37 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 5 additions & 13 deletions src/Entity/BaseUser3.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
54 changes: 54 additions & 0 deletions src/Listener/DoctrineMappingListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* 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';
}
}
2 changes: 1 addition & 1 deletion src/Resources/config/doctrine/BaseUser.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<field name="lastLogin" column="last_login" type="datetime" nullable="true"/>
<field name="confirmationToken" column="confirmation_token" type="string" length="180" unique="true" nullable="true"/>
<field name="passwordRequestedAt" column="password_requested_at" type="datetime" nullable="true"/>
<field name="roles" column="roles" type="array"/>
<field name="roles" column="roles" type="json"/>
<field name="createdAt" type="datetime" column="created_at"/>
<field name="updatedAt" type="datetime" column="updated_at"/>
<lifecycle-callbacks>
Expand Down
22 changes: 0 additions & 22 deletions src/Resources/config/doctrine/BaseUser3.orm.xml

This file was deleted.

9 changes: 9 additions & 0 deletions src/Resources/config/orm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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'),
]);
};

0 comments on commit 85202a9

Please sign in to comment.