試圖讓我的映射器 內最後一個插入ID我有這樣的代碼:獲取最後插入ID ZF2
$sql = new Sql($this->dbAdapter);
$stmt = $sql->prepareStatementForSqlObject($action);
$result = $stmt->execute();
return array('success' => $result->getAdapter()->lastInsertId());
我的問題是,我得到一個致命的錯誤:(!)致命錯誤:調用未定義的方法Zend \ Db \ Adapter \ Driver \ Pdo \ Result :: getAdapter()在/var/www/zf-skeleton/module/Application/src/Application/Mapper/ZendDbSqlMapper.php on line 100
I似乎無法獲得適配器,儘管數據已被添加到數據庫中。
任何幫助表示讚賞。
我的整個類:
/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');
}
}
這不起作用,因爲$ db或$ Table都不是我班的一部分。我更新了我的問題。 – beingalex