是否可以在没有关系数据库的情况下使用Symfony2 / Doctrine表单验证?

时间:2015-01-01 13:36:13

标签: php mysql mongodb symfony doctrine-orm

我有以下控制器操作(简化):

public function createAction()
{
    $request = $this->getRequest();

    $form = $this->createForm(
        new DashboardType(),
        new Dashboard()
    );

    if ($request->isMethod('POST')) {
        $form->bind($request);
        if ($form->isValid()) {
            $dm = $this->get('doctrine_mongodb')
                       ->getManager();
            $dm->persist($dashboard);
            $dm->flush();
        }
    }
}

其中仪表板是Mongo Document对象而不是实体。此代码在$ form->绑定行上失败,并出现以下DBAL错误:

PDOException: could not find driver

我假设这是因为我没有在我的parameters.yml文件中设置关系数据库。我需要的所有这个应用程序是通过Docker运行的Mongo。

如果没有安装数据库,是否可以使用表单验证等功能运行Symfony和Doctrine?有必要停靠和管理一个不会完全用于满足Symfony要求的MySQL实例似乎毫无意义。

有解决方法吗?

更新

这不是驱动程序问题,因为我可以在没有形式值的情况下读取和写入Mongo。来自模块的编辑回复:

$ php -m

[PHP Modules]
mongo
PDO

解决方案

默认情况下,Symfony将以下内容添加到config.yml

# Doctrine Configuration
doctrine:
    dbal:
        driver:   "%database_driver%"
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8
        # if using pdo_sqlite as your database driver:
        #   1. add the path in parameters.yml
        #     e.g. database_path: "%kernel.root_dir%/data/data.db3"
        #   2. Uncomment database_path in parameters.yml.dist
        #   3. Uncomment next line:
        #     path:     "%database_path%"

    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        auto_mapping: true

这会以某种方式影响表单验证器。通过删除该部分并仅具有doctrine mongo配置,错误是固定的。

1 个答案:

答案 0 :(得分:1)

您应该将Symfony配置为使用MongoDB作为持久性系统。

您可以在Symfony doc的DoctrineMongoDBBundle上找到设置过程:

http://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.html

相关问题