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

Make the code compatible with DBAL 3 and 4 by swithing the type when… #1685

Open
wants to merge 1 commit into
base: 5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
rande marked this conversation as resolved.
Show resolved Hide resolved
{
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';
Comment on lines +49 to +52
Copy link
Member

Choose a reason for hiding this comment

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

Is it the right way to change the value ?

The property is documented as READ-ONLY
https://github.com/doctrine/orm/blob/3d9af3187f59930972e7fcb90a1d8059a37b8032/src/Mapping/ClassMetadata.php#L334-L339

Copy link
Member

Choose a reason for hiding this comment

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

I think

public function setAttributeOverride(string $fieldName, array $overrideMapping): void

should be used instead. (https://github.com/doctrine/orm/blob/3.1.x/src/Mapping/ClassMetadata.php#L1737)

Copy link
Member Author

Choose a reason for hiding this comment

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

@VincentLanglet https://github.com/doctrine/orm/blob/3.1.x/src/Mapping/ClassMetadata.php#L1765-L1767 it is not possible to change the type using this way.

This solution is a hack but to be honest having a second class BaseUser3 just to change the type is not good either. This can end up, with BaseUser4 etc ...

Copy link
Member

Choose a reason for hiding this comment

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

Any recommendation for such situation @greg0ire ? Is it the best way ?

}
}
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'),
]);
};
Loading