dtlrp119999 2014-04-24 13:37
浏览 14
已采纳

Zend \ Stdlib \ Hydrator \ ClassMethods extract()返回一个空数组

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?

  • 写回答

1条回答 默认 最新

  • drxrgundk062317205 2014-04-24 14:53
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失