-1

In my "Module.php" i have this code in getServiceConfig()

                'FoobarTableGateway' => function ($sm) {
                $dbAdapter = $sm->get('dbfoobar');
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new Foobar());
                return new TableGateway('foobartable', $dbAdapter, null, $resultSetPrototype);
            },

Now, i have to write a matching Class with an "exchangeArray($data)" method in, where i have to match every single column to a class property. This will work, but:

How do i write this without having to write the matching by hand? Is there some way of "automatching" the column, so i can handle the column 1:1 in my Entity? I think, i have to use something other than setArrayObjectPrototype but i have not found anything on google yet. Any help (some links or so) will do, thanks ;)

Paladin
  • 1,637
  • 13
  • 28

1 Answers1

0

Zf2 provides default hydratation strategies.

$resultSetPrototype = new Zend\Db\ResultSet\HydratingResultSet();
$resultSetPrototype->setHydrator(new Zend\Stdlib\Hydrator\ObjectProperty());
$resultSetPrototype->setObjectPrototype(new Foobar());

The objectProperty type automatically hydrate based on the object attribute. You also have other type like the ClassMethods which would use your getter / setter methods.

$resultSetPrototype = new Zend\Db\ResultSet\HydratingResultSet();
$resultSetPrototype->setHydrator(new Zend\Stdlib\Hydrator\ClassMethods());
$resultSetPrototype->setObjectPrototype(new Foobar());

You can also define your custom hydrator in your resultsetprototype to automatically assign your data to your model. But this solution would require you to write the exact line of code you don't want.

Unex
  • 1,747
  • 13
  • 17
  • Basicly, i just want to get rid of class Foobar – Paladin Aug 25 '16 at 13:09
  • This is your model class, you shouldn't get rid of it. – Unex Aug 25 '16 at 13:25
  • My Models called "FoobarTable", i just want to know, how to get rid off the entity "Foobar" (in some cases, i simple do not need it). – Paladin Aug 25 '16 at 13:38
  • In my example if your model is `FoorbarTable` then you can just use it instead of `Foobar()`. But i don't think foobarTable is a model classe. The name doesn't seems to. – Unex Aug 25 '16 at 13:39
  • It's according to the tutorial from zend: https://framework.zend.com/manual/2.4/en/user-guide/database-and-models.html – Paladin Aug 25 '16 at 14:01
  • Well okay, then with my solution you don't need to do the method exchange array & co. But you need to have the model file with the properties. And use the first hydrator i suggested you. With that, the response from your repository will automatically return model instead of arrays. – Unex Aug 25 '16 at 14:08