0

Is using an aggregate hydrator appropriate for this situation? If so how would I go about hydrating these entities, I found the documentation confusing & can't really find any examples online.

I have two entities with a many-to-many relationship. Some properties on one of the entities are dependant on the relationship.

Example

Here are two entities with a many-to-many relationship, which is maintained using a relationship table in the database. The size property on the question object is dependant on which application the relationship.

diagram

The database is laid out like this. diagram

So the idea is that each application can have multiple questions & the size of each question can be set individually for each application.

My understanding of the aggregate hydrator is that I have a normal hydrator to hydrate both application and question entities initially, then the aggregate would hydrate a final application entity using the two entities it previously hydrated. Is that correct, I could be very wrong.

Is it possible to hydrate objects like this without an aggregate hydrator, what is the best way to do this?

mevans
  • 196
  • 2
  • 9
  • I don't think aggregate hydrators are necessarily better suited to objects with complex relationships. Have you written the PHP entities you wish to hydrate from those tables? – avy Mar 06 '18 at 21:37
  • 1
    Maybe the [Data Mapper](https://martinfowler.com/eaaCatalog/dataMapper.html) pattern would be better for such a complex task. – odan Mar 06 '18 at 21:40
  • Yes, I have written the entities and have also got the database. Just trying to find the best way to get the data from the database, manipulate it and put it into the entities. – mevans Mar 06 '18 at 21:42
  • @DanielO Do you know of any examples using ZF2. From the link you posted about the data mapper pattern, it kinda seems to me that the hydrator fills this role in ZF2? – mevans Mar 06 '18 at 21:45
  • 1
    The Data Mapper completely separates the domain objects from the persistent storage (database) and provides methods that are specific to domain-level operations. Use it to transfer data from the domain to the database and vice versa. Within a method, a database query is usually executed and the result is then mapped (hydrated) to a domain object or a list of domain objects. The Data Mapper only uses the hydrator for the automatic filling of object data. The hydrator is convenient but not obligatory. – odan Mar 06 '18 at 21:55
  • Here is a example of a [Data Mapper without Hydrator](https://stackoverflow.com/a/49058090/1461181). Take a look in the`BookEntity` __construct() method. To automate this task you could use the Zend Hydrator. – odan Mar 06 '18 at 22:03

0 Answers0