无法注册自定义DBAL类型

时间:2017-03-13 17:14:41

标签: php symfony doctrine-orm silex dbal

我试图注册一堆自定义 DBAL 类型。当我运行 migrations:diff 时,我得到了例外:

  

致命错误:Class' App \ Persistence \ Models \ Types \ Money'在第174行的D:\ development \ projects \ project \ vendor \ doctrine \ dbal \ lib \ Doctrine \ DBAL \ Types \ Type.php中找不到

我试图通过在所有Doctrine设置之后注册并使用事件订阅者来执行此操作:

class DoctrineCustomTypesEventSubscriber implements Subscriber {

    public function getSubscribedEvents() {
        return [Events::postConnect];
    }

    public function postConnect(ConnectionEventArgs $args) {
        Type::addType('money', "App\Persistence\Models\Types\Money");
        Type::addType('geopoint', "App\Persistence\Models\Types\Point");
        Type::addType('geoarea', "App\Persistence\Models\Types\Area");
    }

}

$doctrineCustomTypesSubscriber = new App\Persistence\DoctrineCustomTypesEventSubscriber();
$app['db.event_manager']->addEventSubscriber($doctrineCustomTypesSubscriber);

$app->register(new Dflydev\Provider\DoctrineOrm\DoctrineOrmServiceProvider, array(
    'orm.proxies_dir' => $app['APP_ROOT_DIR'].'/app/persistence/proxies',
    'orm.em.options' => array(
        'mappings' => array(
            array(
                'type' => 'annotation',
                'namespace' => 'App\Persistence\Models',
                'path' => $app['APP_ROOT_DIR'].'/app/persistence/models',
                'use_simple_annotation_reader' => false,
            ),
        ),
    ),
));

更新

在所有orm设置之前放置注册也不起作用:

use Doctrine\DBAL\Types\Type;

$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
    'db.options' => array('url' => $app['APP_DB_CONN_URL']),
));

Type::addType('money', "App\Persistence\Models\Types\Money");
Type::addType('geopoint', "App\Persistence\Models\Types\Point");
Type::addType('geoarea', "App\Persistence\Models\Types\Area");

我在这里做错了什么?

您也可以告诉我在哪里注册这些注册:

$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping(...)?

1 个答案:

答案 0 :(得分:1)

类型类应该存在并在自动加载器中注册,以便它们可以由与类型相关联的FQCN实例化。