1

我正在尝试实现一个操作,该操作可以在我插入记录之前让我获得最后插入的 id。

该操作基本上应该获取最后插入的 id,然后我将其加 1,然后该值将用于当前插入的数据。

这是我已经走了多远并且错误越来越多

//the action to get the last inserted id
public function getLastID(){
    $lastcourseid = $this->tableGateway->select(function (Select $select){
        $select->columns(array('id'));
        $select->order('id ASC')->limit(1);
     });

    var_dump($lastcourseid);


    return $lastcourseid;
}

我在保存之前在这里调用函数

        if($id == 0){
        $data['course_code'] = $this->getLastID();
        $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");//an error is thrown in case the id is not found

        }
    }

这是我得到的错误

可捕获的致命错误: Zend\Db\ResultSet\ResultSet 类的对象无法转换为字符串我不知道哪里出错了。

任何帮助将不胜感激。谢谢

4

2 回答 2

4

没有像“插入前的最后一个 id”这样的东西。
而你不需要它。

首先插入一条记录,然后获取您的 id。这就是它的工作原理。

于 2013-07-30T05:15:12.937 回答
0

您没有提到底层数据库(postgressql、mysql 等)。我更熟悉MySQL。Perl 和 php AIP 具有“最后一行 id”功能。例如,php 有“mysqli_insert_id()”。这假定您的 rowID 是一个 AUTO_INCREMENT 列。其他数据库可能有不同的要求。

于 2013-07-30T05:20:28.143 回答