Php 获取最后一个插入ID ZF2
正在尝试获取映射器中的最后一个插入ID 我有以下代码:Php 获取最后一个插入ID ZF2,php,zend-framework,Php,Zend Framework,正在尝试获取映射器中的最后一个插入ID 我有以下代码: $sql = new Sql($this->dbAdapter); $stmt = $sql->prepareStatementForSqlObject($action); $result = $stmt->execute(); return array('success' => $result->getAdapter()->lastIn
$sql = new Sql($this->dbAdapter);
$stmt = $sql->prepareStatementForSqlObject($action);
$result = $stmt->execute();
return array('success' => $result->getAdapter()->lastInsertId());
我遇到的问题是,我得到一个致命错误:(!)致命错误:在第100行的/var/www/zf-skeleton/module/Application/src/Application/Mapper/ZendDbSqlMapper.php中调用未定义的方法Zend\Db\Adapter\Driver\Pdo\Result::getAdapter()
我似乎无法获得适配器,尽管数据正在添加到数据库中
感谢您的帮助
我的全班同学:
/home/alex/development/myapplication/module/
Application/src/Application/Mapper/ZendDbSqlMapper.php:
<?php
namespace Application\Mapper;
use Application\Model\PdiInterface;
use Application\Model\MiscdamageInterface;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\Adapter\Driver\ResultInterface;
use Zend\Db\ResultSet\HydratingResultSet;
use Zend\Db\Sql\Insert;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Update;
use Zend\Stdlib\Hydrator\HydratorInterface;
class ZendDbSqlMapper implements PdiMapperInterface
{
protected $dbAdapter;
protected $hydrator;
protected $pdiPrototype;
protected $miscdamagePrototype;
public function __construct(
AdapterInterface $dbAdapter, HydratorInterface $hydrator, PdiInterface $pdiPrototype, MiscdamageInterface $miscdamagePrototype
) {
$this->dbAdapter = $dbAdapter;
$this->hydrator = $hydrator;
$this->pdiPrototype = $pdiPrototype;
$this->miscdamagePrototype = $miscdamagePrototype;
}
public function find($id)
{
$sql = new Sql($this->dbAdapter);
$select = $sql->select('vehicles');
$select->where(array('id = ?' => $id));
$stmt = $sql->prepareStatementForSqlObject($select);
$result = $stmt->execute();
if ($result instanceof ResultInterface && $result->isQueryResult() && $result->getAffectedRows()) {
return $this->hydrator->hydrate($result->current(), $this->pdiPrototype);
}
throw new \InvalidArgumentException("Vehicle with given ID:{$id} not found.");
}
public function findAll()
{
$sql = new Sql($this->dbAdapter);
$select = $sql->select('vehicles');
$stmt = $sql->prepareStatementForSqlObject($select);
$result = $stmt->execute();
if ($result instanceof ResultInterface && $result->isQueryResult()) {
$resultSet = new HydratingResultSet(new \Zend\Stdlib\Hydrator\ClassMethods(), new \Application\Model\Pdi());
return $resultSet->initialize($result);
}
return array();
}
public function getMiscdamage($id)
{
$sql = new Sql($this->dbAdapter);
$select = $sql->select('misc_damage')->where(array('vehicle_id = ?' => $id))->order('date_added DESC');
$stmt = $sql->prepareStatementForSqlObject($select);
$result = $stmt->execute();
if ($result instanceof ResultInterface && $result->isQueryResult()) {
$resultSet = new HydratingResultSet(new \Zend\Stdlib\Hydrator\ClassMethods(), new \Application\Model\Miscdamage());
return $resultSet->initialize($result);
}
throw new \InvalidArgumentException("Vehicle with given ID:{$id} not found.");
}
public function saveMiscdamage($data)
{
$action = new Insert('misc_damage');
$action->values(
array(
'vehicle_id' => $data->vehicle_id,
'description' => $data->description,
'status' => 0,
'added_user_id' => 1,
'date_added' => date('Y-m-d H:i:s')
)
);
try {
$sql = new Sql($this->dbAdapter);
$stmt = $sql->prepareStatementForSqlObject($action);
$result = $stmt->execute();
return array('success' => $Table->getAdapter()->lastInsertId());
} catch(\Exception $e) {}
throw new \Exception('Database error');
}
}
/home/alex/development/myapplication/module/
Application/src/Application/Mapper/ZendDbSqlMapper.php:
试试这个:
return array('success' => $db->lastInsertId());
或
我想出了如何做到这一点:
$sql = new Sql($this->dbAdapter); // Same
$stmt = $sql->prepareStatementForSqlObject($action); // Same
$result = $stmt->execute(); // Same
return array('success' => $result->getGeneratedValue()); // <-- New
$sql=newsql($this->dbAdapter);//同样的
$stmt=$sql->prepareStatementForSqlObject($action);//同样的
$result=$stmt->execute();//同样的
返回数组('success'=>$result->getGeneratedValue());//这不起作用,因为$db
或$Table
都不是我的类的一部分。我已经更新了我的问题。
$sql = new Sql($this->dbAdapter); // Same
$stmt = $sql->prepareStatementForSqlObject($action); // Same
$result = $stmt->execute(); // Same
return array('success' => $result->getGeneratedValue()); // <-- New