How to adapt modules ZfcUser/ zfcuserDoctrineORM i

2019-03-16 07:50发布

问题:

I’m writing from Argentina, forgive my English little. I’m having some problems with modules ZfcUser and zfcuserDoctrineORM. I need to integrate them into my project. I’m working with Zend framework 2 , doctrine 2.3 and postgreSQL and this is the first time I work with these tools. For that reason, there are many things that I don’t dominate well, I have all the modules included in my /config/application.config.php and my connection is configured in my database in /config/autoload/local.php

Local.php


    return array(
      'doctrine' => array(
        'connection' => array(
            'orm_default' =>array(
                'driverClass' => 'Doctrine\DBAL\Driver\PDOPgSql\Driver',
                    'params' => array(
                        'host'     => 'localhost',
                        'port'     => '5432',
                        'user'     => 'postgres',
                        'password' => 'postgres',
                        'dbname'   => 'ministerio',
                    )
                )
            )
        ),
    );

application.config.php


    return array(
      'modules' => array(
        'Application',
        'DoctrineModule',
        'DoctrineORMModule',
        'Reeser',           // Name of my module
        'ZfcBase',
        'ZfcUser', 
        'ZfcUserDoctrineORM',  

    ),
    'module_listener_options' =>array(
          'config_glob_paths'    =>array(
              'config/autoload/{,*.}{global,local}.php',
        ),
        'module_paths' =>array(
             './module',
             './vendor',
          ),
       ),
    );

In order to map my database I made use of annotations with doctrine and I have my own entity user generated in my module.

I added the configuration archives zfcuser.global.php and zfcuserdoctrineorm.global.php in my autoload directory but I don’t know how to configure them so that the archives recognize my entity.

Into zfcuser.global.php

    'zend_db_adapter' => 'Zend\Db\Adapter\Adapter',    // should this comment it?

    'user_entity_class' => 'Reeser\Entity\User',

    'login_redirect_route' => 'Reeser/index/index.phtml',

    return array(
         'zfcuser' => $settings,        // How I configure this code?
         'service_manager' =>array(     
         'aliases' => array(
         'zfcuser_zend_db_adapter' => (isset($settings['zend_db_adapter'])) ?
         $settings['zend_db_adapter']: 'Zend\Db\Adapter\Adapter',
            ),
         ),
    );  

Into zfcuserdoctrineorm.global.php

    return array(
       'doctrine' => array(
          'driver' => array(
             'zfcuser_driver' =>array(
                 'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
                 'cache' => 'array',
                 'paths' => array(__DIR__ .'/../src/Reeser/Entity')
            ),

            'orm_default' =>array(
                'drivers' => array(
                    'ZfcUser\Entity'  =>  'zfcuser_driver'
                )
            )
         )
      ),
    );

I saw that module zfcuserDoctrineORM works with XML. Can the module be adapted to work with annotations? If this is possible, how do I adapt my entity user to this module? Which archives should I modify ?

回答1:

You don't need to adapt ZfcUserDoctrineORM to use annotation mappings. DoctrineORMModule supports mixed mappings natively (it's your choice to decide which entities to map with which drivers). About ZfcUser's configuration, I personally didn't modify it at all (I only did some overrides on what ZfcUserDoctrineORM does).

  1. remove config/autoload/zfcuser.global.php (you don't need it)
  2. remove config/autoload/zfcuserdoctrineorm.global.php
  3. in the module defining your user entity, use following if you want to override the annotation driver of ZfcUserDoctrineOrm (assuming the file is in 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,
    ),
    

This should work for the 0.1.x versions of ZfcUserDoctrineORM



回答2:

The solution from Ocramius worked for me with a few modifications (big thank you!),

First, there seems to be a bug in the latest release of doctrine-module (I got an error saying 'when requiring zfcuser_doctrine_em, the service cannot be found' ), so I had to revert to 0.7 instead. I have attached my composer.json config below,

"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.*",

The next thing is, I had to keep my zfcuser.global.php with the following config option, 'user_entity_class' => 'Application\Entity\User', This is required if you want to override the default entity with your own one.

Hope this helps.