phpro / zf-doctrine-hydration-module

Configurable Doctrine hydrators for ZF2
18 stars 33 forks source link

Added support for NamingStrategies. #9

Closed boukeversteegh closed 9 years ago

boukeversteegh commented 9 years ago

This allows you to map entity propertynames to keys. Doctrine already supports this, but this module didn't.

For example:

class User {
     protected $id;
     protected $userConnections;
}

mapping it to:

{
  "id": 12,
  "connections": []
}

NamingStrategy class:

namespace Application\Strategies;
use Zend\Stdlib\Hydrator\NamingStrategy\MapNamingStrategy;

class UserNamingStrategy extends MapNamingStrategy {
    protected $mapping = [
        'connections' => 'userConnections',
    ];

    public function __construct() {
        parent::__construct($this->mapping);
    }
}

config file:

return array(
    'doctrine-hydrator' => array(
        'user' => array(
            'entity_class' => 'doctrine.entity',
            'object_manager' => 'doctrine.entitymanager.orm_default',
            'by_value' => true,
            'naming_strategy' => 'Application\Strategies\UserNamingStrategy',
            'strategies' => array(
                'userConnections' => 'Application\Strategies\EmbeddedCollectionStrategy',
            )
        ),
veewee commented 9 years ago

Thanks for contributing! Before I can merge this pull request, can you please make following adjustments:

boukeversteegh commented 9 years ago

I've moved the code to a separate function. I'm not that familiar with phpunit, and i found no current tests for hydratorstrategies, so it might take a while before i do that.

The error in travis is not related to my code, but something about an old nodejs version, so i don't know what's up with that..

veewee commented 9 years ago

Thanks! I fixed Travis and merged this commit into master. I will tag a new version soon!

boukeversteegh commented 9 years ago

Happy to help, thanks for pulling it!