简体   繁体   中英

Using Doctrine Uuid type with Sonata Admin Bundle for Symfony

I'm having trouble making Sonata Admin Bundle working with Ramsey's Uuid as the entity's Id.

This is the error I get when I try to show the Country list (/admins/app/country/list)

在此处输入图像描述

在此处输入图像描述

An exception has been thrown during the rendering of a template ("Unknown database type uuid requested, Doctrine\DBAL\Platforms\MySQL57Platform may not support it.").

doctrine.yaml

doctrine:
    dbal:
        types:
            uuid: Ramsey\Uuid\Doctrine\UuidType

services.yaml

sonata.admin.country:
    class: App\Admin\CountryAdmin
    arguments: [~, App\Entity\Country, ~]
    tags:
        - { name: sonata.admin, manager_type: orm, group: Entities, label: Country }

App\Entity\Country

class Country
{ 
    /**
     * @ORM\Id
     * @ORM\Column(type="uuid", unique=true)
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
     */
    protected $id;

App\Admin\Country

final class CountryAdmin extends Sonata\AdminBundle\Admin\AbstractAdmin
{    
    protected function configureRoutes(RouteCollection $collection)
    {
        $collection
            ->add('show_country', sprintf('%s/show_country', $this->getRouterIdParameter()));
    }
}

App\Controller\Admin\CountryController

class CountryController extends Sonata\AdminBundle\Controller\CRUDController
{
    public function showCityAction(Request $request): Response
    {
        /** @var Country $country */
        $country = $this->admin->getSubject();

        if (!$country) {
            $id = $request->get($this->admin->getIdParameter());
            throw $this->createNotFoundException(sprintf(
                'Unable to find the object with id : %s',
                $id
            ));
        }

        return $this->renderWithExtraParams('admin/city/show_country.html.twig', [
            'show_country' => $country,
        ]);
    }
}

composer.json

    "sonata-project/admin-bundle": "^3.53",
    "sonata-project/doctrine-orm-admin-bundle": "^3.10",
    "sonata-project/translation-bundle": "^2.4",
    "sonata-project/user-bundle": "^4.4",
    "stof/doctrine-extensions-bundle": "^1.3",
    "sonata-project/easy-extends-bundle": "^2.5",
    "ramsey/uuid-doctrine": "^1.5"

It seems that the Uuid type is not loaded, although it's in the doctrine.yaml config file. I tried to include it manually in bootstrap.php:

\Doctrine\DBAL\Types\Type::addType('uuid', 'Ramsey\Uuid\Doctrine\UuidType');

but I just get an error message saying the type is already included.

I realised that vendor/sonata-project/doctrine-extensions/src/Types only contains JsonType.php. Could it be that Uuid.php is missing? Or am I missing something else? Thank you for your help !

I finally found the answer to this. All I had to do is add the 2 lines to the doctrine.yaml file:

doctrine:
    dbal:
        types:
            uuid: Ramsey\Uuid\Doctrine\UuidType
        mapping_types:   <---
            uuid: uuid   <---

I hope it can help someone because personally I couldn't find the information anywhere on the internets !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM