8

I've got an object with values that are stored in my database. My object also contains another object which is stored in the database using just the ID of it (foreign key).

http://framework.zend.com/manual/2.0/en/modules/zend.stdlib.hydrator.html

Before the Hydrator/exchangeArray functionality in ZF2 you would use a Mapper to grab everything you need to create the object. Now I'm trying to eliminate this extra layer by just using Hydration/exchangeArray to populate my objects but am a bit stuck on creating the nested object.

Should my entity have the Inner object's table injected into it so I can create it if the ID of it is passed to my 'exchangeArray' ?

Here are example entities as an example.

// Village
id, name, position, square_id

// Map Square
id, name, type

Upon sending square_id to my Village's exchangeArray() function. It would get the mapTable and use hydrator to pull in the square using the ID I have.

It doesn't seem right to be to have mapper instances inside my entity as I thought they should be disconnected from anything but it's own entity specific parameters and functionality?

creamcheese
  • 2,524
  • 3
  • 29
  • 55
  • did @DrBeza's strategy work for you? i'm asking because you haven't marked it as solution yet :) – Ron Jan 03 '13 at 14:07
  • sort of worked. Creating the custom hydrators for each of my entities are great for hydrating and extracting. I followed this tutorial: http://zf2.readthedocs.org/en/latest/user-guide/database-and-models.html But have to save() my entities across multiple tables and it feels like that doesn't belong in my controller. Would be nice to tell an entity to save and it knows which tables to distribute into. Another of my problems is child objects containing the parent object and hydrating the parents into them... running into circular dependencies this way and figure I shouldn't hydrate parents in – creamcheese Feb 07 '13 at 23:47
  • @DominicWatson, have you figured any way to solve circular dependency problem? I am in a kind of similar situation and injecting my mappers to another to create necessary objects that I want to refer in the base object. Like creating Product object contains group_id and setting ProductGroup by injected GroupMapper in the ProductMapper. However, the other way is also necessary, like I have ProductGroup and need to get group products as Product object, so I need to inject ProductMapper in GroupMapper as well. Of course I get circular dependency and I saw your question, wanted to ask. Thanks. – smozgur Jun 18 '15 at 23:30

1 Answers1

5

I think you may benefit from looking at Zend\Stdlib\Hydrator\Strategy\StrategyInterface.

By creating a Strategy class you can attach this to your Hydrator, so when a specific key is found (square_id in this case) the Hydrator passes the data onto the Strategy class to either extract() or hydrate().

The Strategy class can then do whatever is required to hydrate and extract the data. I use a couple of Strategy classes that simply getArrayCopy()/exchangeArray() and other strategies that hydrate/extract multiple entities.

DrBeza
  • 2,241
  • 1
  • 16
  • 18
  • Thanks :) Seems great. I've got it to extract my data using a custom strategy I created called 'CompositeHydratorStrategy'. In my module.php I add each field name, like "map_squares" and if it's an object or array of objects, it loops through and extracts. I'm not 100% how to hydrate the same as well. Looks like I'll have to have a switch statement inside my custom statement that checks for IDs and hydrates if more data is available or gets everything from a separate call if not. – creamcheese Oct 19 '12 at 23:09
  • 1
    Ugh the term "Strategy Pattern" irritates me to no end. It's the silliest name for a programming methodology that I've ever heard. It's so ambiguous anyway. Seriously. it's meaningless. :P +1 though – Yes Barry Oct 29 '12 at 09:27
  • I tried to get this working but had some problems... maybe you could look into this similar issue: http://stackoverflow.com/questions/14140447/zend-framework-2-hydrator-strategy-doesnt-work – Ron Jan 03 '13 at 14:06
  • Using your advice I've managed to create custom hydrators which do exactly what I need :) I've been using them to extract and pass an associative array to the view and have found that it's the main cause of poor performance. Is it normal to pass entire entity objects to the view? cause extraction is a killer before passing to view :) – creamcheese Jan 20 '13 at 02:59
  • I've used Hydrators and Strategies mostly for working with Form objects, so it tends to just be a few entity objects and therefore I’ve not experienced any noticeable performance hits. Have you tried another Hydrator such as the ArraySerializable? Of course you would need to implement the getArrayCopy and exchangeArray methods but I would expect this to be better performance over the ClassMethods hydrator. – DrBeza Jan 21 '13 at 17:14
  • Sorry to revive a slightly old question, but @DrBeza do you have any examples online for using hydrators and strategies for getting multiple entities out? Thanks – Gabriel Baker Mar 08 '13 at 16:38
  • Unfortunately I do not at this time. The project in which I used strategies (mostly with forms), we had two main types of strategy objects. One was for handling single nested records and one for handling multiple nested records. In the case of the strategy dealing with the nested records both hydrate/extract would be passed multiple items in an array. Both methods would iterate over these items and either hydrate or extract the data and then return the output. More recently I’ve not been using ZF2 forms, so I’m not sure if there has been changes with this. – DrBeza Mar 08 '13 at 17:10