Reputation: 433
i'm trying to implement the RowGateway
class to my entities, I already have a form working with the entity and I'm trying to set the hydrator to work with ClassMethods
.
I also noticed that ArraySerializable
hydrator calls the populate()
method or exchangeArray()
and this method set the appropriate primary key when editing a row, unfortunately ClassMethods
Hydrator doesn't do that.
What would be the best way to set the correct primary key value when using the Classmethod hydrator, should I set this value before binding the entity to the form? Or, should I extend the Classmethod H. to perform this task on initialize?
Upvotes: 2
Views: 1899
Reputation: 792
I'm not fond of using knowledge of the data layer in my entity. When using exchangeArray()
you create mapping in the entity itself. I did some research about Zend's hydrators and came across serval posts including this one. Andrew's example of extending the ClassMethods
hydrator seemed a good approach to map column names to getters/setters names.
When extending the ClassMethods
hydrator you could also implement Zend\Stdlib\Hydrator\HydratorInterface
.
For data manipulation use hydrator strategies.
http://framework.zend.com/manual/2.0/en/modules/zend.stdlib.hydrator.strategy.html http://juriansluiman.nl/nl/article/125/strategies-for-hydrators-a-practical-use-case
To sepperate your entity over mutliple data sources you can use hydrator filters. For example, by default the ClassMethods
hydrator extracts all entity methods starting with get.
http://framework.zend.com/manual/2.1/en/modules/zend.stdlib.hydrator.filter.html
Upvotes: 3
Reputation: 12809
You could extend Zend\Stdlib\Hydrator\ClassMethods and do any transformations you require here, assuming this is what you mean.
You can then use mapField to map from one of your fields to the correct id field name.
namespace Application\Model;
use Zend\Stdlib\Hydrator\ClassMethods;
class MyHydrator extends ClassMethods
{
/**
* Extract values from an object
*
* @param object $object
* @return array
* @throws Exception\InvalidArgumentException
*/
public function extract($object)
{
$data = parent::extract($object);
$data = $this->mapField('id', 'user_id', $data);
return $data;
}
/**
* Map fields
*
* @param type $keyFrom
* @param type $keyTo
* @param array $array
* @return array
*/
protected function mapField($keyFrom, $keyTo, array $array)
{
$array[$keyTo] = $array[$keyFrom];
unset($array[$keyFrom]);
return $array;
}
}
Alternatively you could make a getter and setter for the id field you need setting/getting, for example if you have an id called 'user_id' :
public function getUserId() { .. }
public function setUserId($id) { .. }
Upvotes: 1