DASPRiD / container-interop-doctrine

Doctrine factories for container-interop
107 stars 25 forks source link

Type is not registered by container-interop-doctrine #38

Closed ma-si closed 6 years ago

ma-si commented 6 years ago

:beetle: Types defined in the configuration are not registered.

Example uses

Using the Doctrine CLI

$ ./vendor/bin/doctrine orm:validate-schema

Mapping
-------
 [FAIL] The entity-class Api\Books\Entity\Book mapping is invalid:
 * The field 'Api\Books\Entity\Book#id' uses a non-existent type 'uuid'.

Database
--------
In DBALException.php line 283:
  Unknown column type "uuid" requested. Any Doctrine type that you use has to be registered with \Doctrine\DBAL\Types\Type::  
  addType(). You can get a list of all the known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs d  
  uring database introspection then you might have forgotten to register all database types for a Doctrine Type. Use Abstrac  
  tPlatform#registerDoctrineTypeMapping() or have your custom types implement Type#getMappedDatabaseTypes(). If the type nam  
  e is empty you might have a problem with the cache or forgot some mapping information.

Configuration

// doctrine.global.php
<?php

declare(strict_types=1);

return [
    'doctrine' => [
        'configuration' => [
            'orm_default' => [
                'types' => [
                    \Ramsey\Uuid\Doctrine\UuidType::NAME => \Ramsey\Uuid\Doctrine\UuidType::class,
                ],
            ],
        ],
        'eventmanager' => [
            'orm_default' => [
                'subscribers' => [
                    'Gedmo\Tree\TreeListener',
                    'Gedmo\Timestampable\TimestampableListener',
                    'Gedmo\Sluggable\SluggableListener',
                    'Gedmo\Loggable\LoggableListener',
                    'Gedmo\Sortable\SortableListener',
                ],
            ],
        ],
    ],
];

Entity

<?php

declare(strict_types=1);

namespace Api\Books\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Ramsey\Uuid\UuidInterface;

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

    [...]

Temp solution :hankey:

Manually addType()

// cli-config.php
<?php

declare(strict_types=1);

$container = require 'container.php';

/**
 * @FIXME Type is not registered by container-interop-doctrine
 * //doctrine.global.php
 * ```
 * return [
 *     'doctrine' => [
 *         'configuration' => [
 *             'orm_default' => [
 *                 'types' => [
 *                     \Ramsey\Uuid\Doctrine\UuidType::NAME => \Ramsey\Uuid\Doctrine\UuidType::class,
 *                 ],
 *             ],
 *         ],
 *     ],
 * ];
 * ```
 */
\Doctrine\DBAL\Types\Type::addType(\Ramsey\Uuid\Doctrine\UuidType::NAME, \Ramsey\Uuid\Doctrine\UuidType::class);

return new \Symfony\Component\Console\Helper\HelperSet([
    'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper(
        $container->get('doctrine.entity_manager.orm_default')
    ),
]);
DASPRiD commented 6 years ago

Not an issue. Types are not registered for a specific entity manager instance but globally, so the 'types' array should be directly under 'doctrine'. See:

https://github.com/DASPRiD/container-interop-doctrine/blob/master/example/full-config.php#L121

twmobius commented 5 years ago

I've tried setting the UuidType to 'types' array directly under 'doctrine' but it didn't work. Does it actually work?

DASPRiD commented 5 years ago

Yes it does, I use it on every project.

wuestkamp commented 5 years ago

Same here, this config didn't work for APP_ENV=prod:

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

file config/packages/doctrine.yaml

for prod no config is overridden. I had to manually add this:

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

to bin/console.php and public/index.php

DASPRiD commented 5 years ago

Well yes, types goes under doctrine, not under dbal.

wuestkamp commented 5 years ago

Not according to https://github.com/ramsey/uuid-doctrine/blob/master/README.md or https://stackoverflow.com/questions/49839886/register-custom-doctrine-type-in-symfony4 ?

DASPRiD commented 5 years ago

Because their example is not for container-interop-doctrine.

DASPRiD commented 5 years ago

That error is not coming from container-interop-doctrine :)