manuakasam / join-hydrator
A set of two very lightweight hydrator components. The main usage of those hydrators would be to hydrate query results from joined tables into their respective nested objects.
dev-master
2015-03-16 11:49 UTC
Requires
- php: >=5.4
- zendframework/zend-eventmanager: 2.*
- zendframework/zend-filter: 2.*
- zendframework/zend-servicemanager: 2.*
- zendframework/zend-stdlib: 2.*
Requires (Dev)
- phpunit/phpunit: 4.5.*
- satooshi/php-coveralls: ~0.6
- squizlabs/php_codesniffer: ~2.3
This package is not auto-updated.
Last update: 2024-11-09 18:06:21 UTC
README
A set of two very simple hydrator helpers to easily hydrate joined query results into their respective nested objects.
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() }