Reputation: 2476
Trying to get the last insert ID inside my mapper I have this code:
$sql = new Sql($this->dbAdapter);
$stmt = $sql->prepareStatementForSqlObject($action);
$result = $stmt->execute();
return array('success' => $result->getAdapter()->lastInsertId());
The problem I have is that I get a fatal error: ( ! ) Fatal error: Call to undefined method Zend\Db\Adapter\Driver\Pdo\Result::getAdapter() in /var/www/zf-skeleton/module/Application/src/Application/Mapper/ZendDbSqlMapper.php on line 100
I don't seem to be able to get the adapter, although the data IS getting added to the database.
Any help is appreciated.
My entire class:
/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');
}
}
Upvotes: 0
Views: 1256
Reputation: 2476
I worked out how to do this:
$sql = new Sql($this->dbAdapter); // Same
$stmt = $sql->prepareStatementForSqlObject($action); // Same
$result = $stmt->execute(); // Same
return array('success' => $result->getGeneratedValue()); // <-- New
Upvotes: 1
Reputation: 1025
Try this:
return array('success' => $db->lastInsertId());
Or
return array('success' => $Table->getAdapter()->lastInsertId());
Upvotes: 0