2

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?

Community
  • 1
  • 1
Brock
  • 1,635
  • 2
  • 18
  • 27

2 Answers2

6

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.

Crisp
  • 11,417
  • 3
  • 38
  • 41
  • But topic continues. What if I want to use column name "id" not "user_id" or changing any other file that can not be set in config? BTW even if I change the entity via config, where is the best place to store my custom UserEntity? Thanks! – Brock Mar 27 '13 at 17:50
  • 1
    If you wanted to used different column names you'd swap out the zfcuser_user_mapper service for one of your own. Typically, you'd do this in your own module and supply the classes and config to over-ride zfcusers settings and services in there. – Crisp Mar 27 '13 at 18:23
  • Hm, could you point to the code sample please? I'm really new to ZF2 ways :). Thanks in advance! – Brock Mar 28 '13 at 08:41
  • 2
    It's hard to find any good examples. Check out the zfcuser wiki https://github.com/ZF-Commons/ZfcUser/wiki/_pages . Take a look at all the questions on SO that have been asked about zfcuser http://stackoverflow.com/search?q=zfcuser+is%3Aquestion . Look at other modules which extend or otherwise interact with zfcuser, such as, zfcUserDoctrineORM found at https://github.com/ZF-Commons or the ScnSocialAuth module at https://github.com/SocalNick/ScnSocialAuth – Crisp Mar 28 '13 at 14:04
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.
Community
  • 1
  • 1
Brock
  • 1,635
  • 2
  • 18
  • 27