manuakasam / join-hydrator

A set of two very simple hydrator helpers to easily hydrate joined query results into their respective nested objects.

Home Page:http://samminds.com/2015/03/using-hydrator-for-join-queries/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Additional Hydrator Components

A set of two very simple hydrator helpers to easily hydrate joined query results into their respective nested objects.

SensioLabsInsight Scrutinizer Code Quality Build Status Coverage Status

Installation

Installation is best done using composr

composer require manuakasam/join-hydrator

If asked for a version choose dev-master

Usage examples

The usage can easily be checked within the /test folder. But here they are again:

Standalone Usage

<?php
namespace Test {
    use Sam\Hydrator\AggregateHydrator;
    use Sam\Hydrator\OneToOneHydrator;
    
    $data = [
        'foo_one' => 'TEST FOO ONE',
        'foo_two' => 'TEST FOO TWO',
        'bar_one' => 'BAR FIGHT ONE',
        'bar_two' => 'BAR FIGHT TWO',
        'baz'     => 'LONELY BAZ'
    ];
    
    $aggregateHydrator = new AggregateHydrator(
        new ClassMethods(),
        new OneToOneHydrator('foo_', 'setFoo', new Foo()),
        new OneToOneHydrator('bar_', 'setBar', new Bar())
    );
    
    $dummy = $aggregateHydrator->hydrate($data, new Dummy());
    
    get_class($dummy);           // instanceof Dummy
    get_class($dummy->getFoo()); // instanceof Foo()
    get_class($dummy->getBar()); // instanceof Bar()
}

Usage with Zend\Db\TableGateway

namespace Test {
    use Zend\Db\Adapter\Adapter;
    use Zend\Db\ResultSet\HydratingResultSet;
    use Zend\Db\Sql\Select;
    use Zend\Db\TableGateway\TableGateway;
    use Sam\Hydrator\AggregateHydrator;
    use Sam\Hydrator\OneToOneHydrator;

    $adapter = new Adapter([
        'driver'         => 'Pdo',
        'username'       => 'admin',  //edit this
        'password'       => 'admin',  //edit this
        'dsn'            => 'mysql:dbname=someDB;host=localhost',
        'driver_options' => array(
            \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
        )
    ]);

    $prototype = new HydratingResultSet(new AggregateHydrator(
        new ClassMethods(),
        new OneToOneHydrator('foo_', 'setFoo', new Foo()),
        new OneToOneHydrator('bar_', 'setBar', new Bar())
    ), new Dummy());
    
    $gateway   = new TableGateway('dummies', $adapter, null, $prototype);

    $resultSet = $gateway->select(function (Select $select) {
        $select->join('foo', 'clients.foo_id = foo.id', [
            'foo_id'   => 'id',
            'foo_bar'  => 'foo_bar',
            'foo_baz'  => 'foo_baz',
        ]);
        $select->join('bar', 'dummies.bar_id = bar.id', [
            'bar_id'   => 'id',
            'bar_bar'  => 'bar_bar',
            'bar_baz'  => 'bar_baz',
        ]);
    });

    $entryOne = $resultSet->current();
    
    get_class($entryOne);           // instanceof Dummy()
    get_class($entryOne->getFoo()); // instanceof Foo()
    get_class($entryOne->getBar()); // instanceof Bar()
}

About

A set of two very simple hydrator helpers to easily hydrate joined query results into their respective nested objects.

http://samminds.com/2015/03/using-hydrator-for-join-queries/


Languages

Language:PHP 100.0%