ZF2 Hydrators...

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

ZF2 Hydrators...

travis
This post has NOT been accepted by the mailing list yet.
I have just recently started to use hydrators to populate my entities in a fashion similar to how zfcUser does, but I have some questions about a more complicated approach.

Whether it's good form or not, I would prefer to have an entity that has other entities, potentially, as properties.  For example, I have an event entity that has a status.  The status is represented by a status entity.  The status entity is simply an ID and a name/description.

So, when I build my select to query the events table in the database, I join my status table, asking for the status ID and the status name/description.  These fields are fetched along with the event's fields.

So, using a simple extension of the ClassMethods hydrator (a la zfcUser), instantiating and populating the event entity is easy.  The problem comes along when I have to populate the status property.

Here's what I did:
 - I wrote a status hydrator.  It's more or less the same, but it first makes a copy of the $data array to work with so as not to "dirty" any other hydrators that might use the initial $data array to hydrate.  (For example, all of my entities have a setId() method, but my queries will return id for the event ID and statusId for the status ID.  I use a mapField() method (see zfcUser) to map statusId to id for the status, but that overwrites the $data array's id value with that of the status ID.  Not good.)
 - I extended the ClassMethods constructor to take in a status hydrator that simply hydrates a status entity.
 - I have the event hydrator add a status element to the array with the result of the status hydrator's hydrate() call.

Here's the event hydrator code (remember that $this->statusHydrator is injected in the constructor):
    public function hydrate(array $data, $object)
    {
        if (!$object instanceof EventEntityInterface) {
            throw new Exception\InvalidArgumentException('$object must be an instance of Entity\EventInterface');
        }

        $status = new Status();
        $data['status'] = $this->statusHydrator->hydrate($data, $status);

        return parent::hydrate($data, $object);
    }

Also, here's the same method for the status hydrator showing the substitution of the $data array:
    public function hydrate(array $data, $object)
    {
        if (!$object instanceof StatusEntityInterface) {
            throw new Exception\InvalidArgumentException('$object must be an instance of Entity\StatusInterface');
        }
        $statusData = $data;
        $statusData = $this->mapField('statusId', 'id', $statusData);
        $statusData = $this->mapField('statusName', 'name', $statusData);
        return parent::hydrate($statusData, $object);
    }

So...my questions:
 - Is this a reasonable way to do this?
 - Is there a better, best-practice approach?  Links?
 - Should my entities mirror DB tables?  (I don't think so, but would welcome arguments to the contrary.)
 - Is there a better hydrator to use (that's already available in the latest ZF2 library)?
 - Is it fair to inject a/the status hydrator into the event hydrator?
 - Is there a better way to protect the $data array values from being "dirtied" by "sub-hydrators"...?

Basically...let me know if I'm on the right track or throw rocks at what I'm trying to accomplish here.  I like using the hydrators thus far, but want to make sure I'm using them in a good way before I get too much further along the path that I'm on.

Thanks in advance...
Reply | Threaded
Open this post in threaded view
|

Re: ZF2 Hydrators...

intellix
This post has NOT been accepted by the mailing list yet.
I'm also having the exact same issue/question.