문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top