【问题标题】:How to adapt modules ZfcUser/ zfcuserDoctrineORM in my project with doctrine 2 using annotations?如何使用注释在我的项目中使用学说 2 调整模块 ZfcUser/zfcuserDoctrineORM?
【发布时间】:2013-05-10 06:51:04
【问题描述】:

我是从阿根廷写的,请原谅我的英语小。我在使用模块 ZfcUserzfcuserDoctrineORM 时遇到了一些问题。我需要将它们集成到我的项目中。我正在使用 Zend 框架 2、教义 2.3 和 postgreSQL,这是我第一次使用这些工具。由于这个原因,有很多事情我没有掌握好,我的/config/application.config.php中包含所有模块,我的连接在/config/autoload/local.php的数据库中配置

Local.php

返回数组( '教义' => 数组( '连接' => 数组( 'orm_default' =>数组( 'driverClass' => 'Doctrine\DBAL\Driver\PDOPgSql\Driver', '参数' => 数组( '主机' => '本地主机', '端口' => '5432', '用户' => 'postgres', '密码' => 'postgres', 'dbname' => 'ministerio', ) ) ) ), );

application.config.php

返回数组( '模块' => 数组( '应用', '教义模块', 'DoctrineORMModule', 'Reeser', // 我的模块名称 'ZfcBase', 'Zfc用户', 'ZfcUserDoctrineORM', ), 'module_listener_options' =>数组( 'config_glob_paths' =>数组( 'config/autoload/{,*.}{global,local}.php', ), 'module_paths' =>数组( '。/模块', '。/小贩', ), ), );

为了映射我的数据库,我使用了带有原则的注释,并且在我的模块中生成了我自己的实体用户。

我在自动加载目录中添加了配置档案 zfcuser.global.phpzfcuserdoctrineorm.global.php,但我不知道如何配置它们以便档案识别我的实体。

进入zfcuser.global.php

'zend_db_adapter' => 'Zend\Db\Adapter\Adapter', // 应该注释掉吗? 'user_entity_class' => 'Reeser\Entity\User', 'login_redirect_route' => 'Reeser/index/index.phtml', 返回数组( 'zfcuser' => $settings, // 我如何配置这段代码? 'service_manager' =>数组( '别名' => 数组( 'zfcuser_zend_db_adapter' => (isset($settings['zend_db_adapter'])) ? $settings['zend_db_adapter']: 'Zend\Db\Adapter\Adapter', ), ), );

进入zfcuserdoctrineorm.global.php

返回数组( '教义' => 数组( '驱动程序' => 数组( 'zfcuser_driver' =>数组( 'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver', '缓存' => '数组', '路径' => 数组(__DIR__ .'/../src/Reeser/Entity') ), 'orm_default' =>数组( '驱动程序' => 数组( 'ZfcUser\Entity' => 'zfcuser_driver' ) ) ) ), );

我看到模块 zfcuserDoctrineORM 与 XML 一起工作。 该模块可以适应注释吗?如果这是可能的,我如何使我的实体用户适应这个模块?我应该修改哪些档案?

【问题讨论】:

    标签: annotations doctrine zfcuser


    【解决方案1】:

    您无需调整 ZfcUserDoctrineORM 即可使用注释映射。 DoctrineORMModule 原生支持混合映射(您可以选择将哪些实体与哪些驱动程序进行映射)。关于ZfcUser的配置,我个人完全没有修改(我只是对ZfcUserDoctrineORM做了一些覆盖)。

    1. 删除config/autoload/zfcuser.global.php(你不需要它)
    2. 删除config/autoload/zfcuserdoctrineorm.global.php
    3. 在定义您的用户实体的模块中,如果您想覆盖 ZfcUserDoctrineOrm 的注释驱动程序,请使用以下内容(假设文件在 YourModule/config/module.config.php 中):

      // entity mappings
      'doctrine' => array(
          'driver' => array(
              'zfcuser_entity' => array(
                  // customize path
                  'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
                  'paths' => array(__DIR__ . '/../src/YourModule/Entity'),
              ),
              'orm_default' => array(
                  'drivers' => array(
                      'YourModule\Entity' => 'zfcuser_entity',
                  ),
              ),
          ),
      ),
      
      // ZfcUser specific config
      'zfcuser' => array(
          'user_entity_class'       => 'YourModule\Entity\User',
          'enable_default_entities' => false,
      ),
      

    这应该适用于ZfcUserDoctrineORM0.1.x 版本

    【讨论】:

    • 这个可行,但是'zfcuser_doctrine_em' => 'Doctrine\ORM\EntityManager'需要在module.config.php中添加到'service_manager' => 'aliases',否则会出现An alias "zfcuser_doctrine_em" was requested but no service could be found异常。
    • 这对我有用。谢谢!恕我直言,这应该被标记为正确答案。
    【解决方案2】:

    Ocramius 的解决方案对我进行了一些修改(非常感谢!),

    首先,最新版本的学说模块中似乎有一个错误(我收到一条错误消息,说“当需要 zfcuser_doctrine_em 时,找不到服务”),所以我不得不恢复到 0.7。我在下面附上了我的 composer.json 配置,

    "doctrine/dbal": "2.3.*",
    "doctrine/common": "2.3.*",
    "doctrine/doctrine-module": "0.7.*",
    "doctrine/doctrine-orm-module": "0.7.*",
    "doctrine/orm": "2.3.*",
    "zf-commons/zfc-user": "0.1.*",
    "zf-commons/zfc-user-doctrine-orm": "0.1.*",
    

    接下来,我必须使用以下配置选项保留我的 zfcuser.global.php, 'user_entity_class' => 'Application\Entity\User', 如果您想用自己的实体覆盖默认实体,这是必需的。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-06
      • 1970-01-01
      • 2015-05-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多