Question

I previously asked a question about how to efficiently do an insert\update with a tablegateway and was informed of Zend\Stdlib\Hydrator\ClassMethods as in this code from the docs:

public function saveAlbum(Album $album)
{
    $data = array(
        'artist' => $album->artist,
        'title'  => $album->title,
    );

    $id = (int)$album->id;
    if ($id == 0) {
        $this->tableGateway->insert($data);
    } else {
        if ($this->getAlbum($id)) {
            $this->tableGateway->update($data, array('id' => $id));
        } else {
            throw new \Exception('Form id does not exist');
        }
    }
}

Could be replaced with this code:

saveAlbum(Album $album) 
{
    $hydrator   = new ClassMethods(false);
    $data = $hydrator->extract($album);
    ...

But I found when using $hydrator->extract($album); it returns an empty array, what could be the cause of this? I did a var_dump() on the $album object being passed into the function and its seems valid. Is there something else I have to do to get this to work?

Was it helpful?

Solution

The class methods hydrator assumes you have getXXX() methods for each property, but the class in your previous question does not. You might want to try the Zend\Stdlib\Hydrator\ObjectProperty hydrator instead, this should return an array using the public properties from the object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top