Andres Echeverry
Andres Echeverry

Reputation: 89

zfcuser + doctrine custom user entity

I'm working on a project with zf2, and the zfcuser module with doctrine. I have created a custom user module that extends zfcuser, also a custom entity for the user table, and make all the necessary changes for the integration. But my problem is when trying to authenticate myself, I get this error:

An alias "Zend\Db\Adapter\Adapter" was requested but no service could be found.

This happens when zfcuser_user_mapper attempts to change the adapter.

Note: I am not very clear why I need to use the Zend \ Db \ Adapter \ Adapter, since I am working with doctrine.

This is the code in the module.php of the custom user module.

    public function getServiceConfig() {
    return [
        'aliases' => array(
            'zfcuser_zend_db_adapter' => 'Zend\Db\Adapter\Adapter',
        ),
        'factories' => [
            'usuario_login_form' => 'Usuario\Factory\Form\Login',
            'usuario_registro_form' => 'Usuario\Factory\Form\Register',
            'usuario_user_service' => 'Usuario\Factory\Service\UserFactory',
            //'usuario_user_mapper' => 'Usuario\Factory\Mapper\User',
            'usuario_auth_service' => 'Usuario\Factory\AuthenticationService',
            'Usuario\Authentication\Adapter\Db' => 'Usuario\Factory\Authentication\Adapter\DbFactory',
            'Usuario\Authentication\Storage\Db' => 'Usuario\Factory\Authentication\Storage\DbFactory',
            //'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\Adapter',
            'usuario_user_mapper' => function ($sm) {
                $mapper = new Mapper\User();
                $mapper->setDbAdapter($sm->get('zfcuser_zend_db_adapter'));
                $mapper->setEntityPrototype(new ORM\Entity\Usuarios());
                $mapper->setHydrator(new \ZfcUser\Mapper\UserHydrator());
                return $mapper;
            },
        ]
    ];
}

This is my global.php file

return array(
'doctrine' => array(
    'connection' => array(
        'orm_default' => array(
            'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
            'params' => array(
                'host' => 'localhost',
                'port' => '3306',
                'user' => 'root',
                'password' => 'toor',
                'dbname' => 'deporte',
            )
        )
    )
),

);

This is my module.config.php file:

    'controllers' => array(
),
'doctrine' => array(
    'driver' => array(
        // overriding zfc-user-doctrine-orm's config
        'usuario_entity' => array(
            'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
            'paths' => __DIR__ . '/../src/Usuario/ORM/Entity',
        ),
        'orm_default' => array(
            'drivers' => array(
                'Usuario\ORM\Entity' => 'usuario_entity',
            ),
        ),
    ),
),
'zfcuser' => array(
    'auth_adapters' => array(100 => 'Usuario\Authentication\Adapter\Db'),
    // telling ZfcUser to use our own class
    'user_entity_class' => 'Usuario\ORM\Entity\Usuarios',
    // telling ZfcUserDoctrineORM to skip the entities it defines
    'enable_default_entities' => false,
),

I thank you for the help, I have already been with this error for days. Thank you very much, and excuse my English.

Upvotes: 1

Views: 300

Answers (1)

Pardeep Kumar
Pardeep Kumar

Reputation: 1819

If you want to change the Entity and want to use your then use following steps: if the zfcuser.global.php file which is placed in config/autoload folder (if not then you can copy if from zfcuser module.

In this global file search for "user_entity_class" key and define your own entity class.By default it uses

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

Like I am using it for Employee entity

'user_entity_class' => 'Employee\Entity\Employee',

In this entity you need to implement UserInterface.

use ZfcUser\Entity\UserInterface;
/**
 * Employee
 *
 * @ORM\Table(name="employee")
 * @ORM\Entity(repositoryClass="Employee\Repository\EmployeeRepository")
 */
class Employee implements UserInterface {

}

If you want to override db adapter then you need to do following steps:

'service_manager' => array(
        'invokables' => array(
            'ZfcUser\Authentication\Adapter\Db' => 'Employee\Authentication\Adapter\Db',
        ),
    ),

In this file you need to extend and implements.

namespace Employee\Authentication\Adapter;

use InvalidArgumentException;
use Zend\Authentication\Result as AuthenticationResult;
use Zend\Crypt\Password\Bcrypt;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use Zend\Session\Container as SessionContainer;
use ZfcUser\Authentication\Adapter\AdapterChainEvent as AuthenticationEvent;
use ZfcUser\Entity\UserInterface as UserEntity;
use ZfcUser\Mapper\HydratorInterface as Hydrator;
use ZfcUser\Mapper\UserInterface as UserMapper;
use ZfcUser\Authentication\Adapter\AbstractAdapter;
use ZfcUser\Options\AuthenticationOptionsInterface as AuthenticationOptions;

class Db extends AbstractAdapter implements ServiceManagerAwareInterface
{

}

For more information you can follow zfcuser wiki here: https://github.com/ZF-Commons/ZfcUser/wiki

Upvotes: 1

Related Questions