Question

I did read this answer ZF2, what's the best practice for working with Vendor Module's Form classes?. So it's mainly clear how to change configurable parts of the vendor module, but what do I do if in zfcUser module I want to add new functionality to the Entity/User?

In short I want to check user role, I've added to DB field, what's the best way to do that? Maybe I should do it somewhere else and not in zfcUSer if so where?

Was it helpful?

Solution 2

I won't be claiming this is a best practice, but this is how I managed to handle the situation! First - big thanks to answer in this question ZF2: Custom user mapper for ZfcUser module.

So, I did:

  1. Added configuration of zfcUser/Entity in zfc-user.global.php autoload:
    'zfcuser' => array(
        'tableName' => 'users',
        'user_entity_class' => 'Application\Entity\User'
    ),
    
  2. Added my classes for Entity\User, Mapper\User and Mapper\Hydrator into my src/Application Entity and Mapper subfolders.
  3. Updated those classes namespaces to be my Application\Entity and Application\Mapper
  4. Added this configuration to getServiceConfig function of my Module.php:

    'factories' => [
        'zfcuser_user_mapper' => function ($sm) {
            $mapper = new Mapper\User();
            $mapper->setDbAdapter($sm->get('Zend\Db\Adapter\Adapter'));
            $mapper->setEntityPrototype(new Entity\User());
            $mapper->setHydrator(new Mapper\UserHydrator());
            return $mapper;
        },
    ]
    
  5. Updated my classes dependencies to use ZfcUSer and started my Entity\User as extends \ZfcUser\Entity\User implements \ZfcUser\Entity\UserInterface.

OTHER TIPS

Take a look at ZfcUser/config/zfcuser.global.php.dist

In there you'll see this

/**
 * User Model Entity Class
 *
 * Name of Entity class to use. Useful for using your own entity class
 * instead of the default one provided. Default is ZfcUser\Entity\User.
 * The entity class should implement ZfcUser\Entity\UserInterface
 */
//'user_entity_class' => 'ZfcUser\Entity\User',

The directions are straightforward, copy the file to ./config/autoload/zfcuser.global.php, makes sure your entity class implements the ZfcUser\Entity\UserInterface, uncomment that line and change the FQCN to match the entity you want to use instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top