Hydrator Strategies

api-skeletons/zf-doctrine-hydrator

A collection of hydrator strategies for phpro/zf-doctrine-hydration-module written for use with zfcampus/zf-apigility-doctrine

Installation

Installation of this module uses composer. For composer documentation, please refer to getcomposer.org:

composer require api-skeletons/zf-doctrine-hydrator "*"

Add this module to your application’s configuration:

'modules' => array(
   ...
   'ZF\Doctrine\Hydrator',
),

StrategyCollectionExtract

Extract a collection. Often when this is used the entities in the collection have a many to one relationship with the entity the collection belongs to. In this case it is recommended you use the EntityLink for the parent on the child.

How to use these hydration strategies

In your configuration for your Doctrine in Apigility API the doctrine-hydrator section add a strategy to an entity. In this example an Artist is a member of a Band which, by example, is a many to one relationship (an artist can only have one band):

'doctrine-hydrator' => array(
    'DatabaseApi\\V1\\Rest\\Album\\SongHydrator' => array(
        'entity_class' => 'Database\\Entity\\Song',
        'object_manager' => 'doctrine.entitymanager.orm_default',
        'by_value' => true,
        'use_generated_hydrator' => true,
    ),
    'DatabaseApi\\V1\\Rest\\Album\\AlbumHydrator' => array(
        'entity_class' => 'Database\\Entity\\Album',
        'object_manager' => 'doctrine.entitymanager.orm_default',
        'by_value' => true,
        'strategies' => array(
            'artist' => 'ZF\Doctrine\Hydrator\Strategy\EntityLink',
            'song' => 'ZF\Doctrine\Hydrator\Strategy\CollectionLink',
        ),
        'use_generated_hydrator' => true,
    ),
    'DatabaseApi\\V1\\Rest\\Artist\\ArtistHydrator' => array(
        'entity_class' => 'Database\\Entity\\Artist',
        'object_manager' => 'doctrine.entitymanager.orm_default',
        'by_value' => true,
        'strategies' => array(
            'album' => 'ZF\Doctrine\Hydrator\Strategy\CollectionExtract',
        ),
        'use_generated_hydrator' => true,
    ),

When an Artist is queried all Albums for the Artist will be returned. For each Album the Artist will be returned only as a self link. The result of a call to https://api/artist/1 will look like:

{
    "id": 1,
    "name": "Soft Cell",
    "_embedded": {
        "album": [
            {
                "id": 1,
                "name": "Non-Stop Erotic Cabaret",
                "_embedded": {
                    "artist": {
                        "_links": {
                            "self": "https://api/artist/1"
                        }
                    },
                    "song": {
                        "_links": {
                            "self": "https://api/song?filter%5B0%5D%5Bfield%5D=album&filter%5B0%5D%5Btype%5D=eq&filter%5B0%5D%5Bvalue%5D=1"
                        }
                    }
                },
                "_links": {
                    "self": "https://api/album/1"
                }
            }
        ],
    },
    "_links": {
        "self": "https://api/artist/1"
    }
}

Note

Maintained by API Skeletons.