Search code examples
phpzend-framework2zfcuser

ZF2 best practice to change core classes of the vendor module?


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?


Solution

  • 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.