php - ZEND2 - lastInsertValue returns NULL -
my table looks this:
create table data.brand_list ( id bigserial not null, brand_name text, created_at timestamp time zone default now(), ghost boolean, constraint brand_list_id_pkey primary key (id) )
it has sequence:
create sequence data.brand_list_id_seq increment 1 minvalue 1 maxvalue 9223372036854775807 start 649 cache 1; alter table data.brand_list_id_seq
my code in zend model:
public function addrow(brandlist $brandlist) { $data = array( 'id' => $brandlist->id, 'brand_name' => $brandlist->brand_name, 'created_at' => $brandlist->created_at, 'ghost' => $brandlist->ghost, ); $id = (int)$brandlist->id; if ($id == 0) { unset($data['id']); $row = $this->tablegateway->insert($data); var_dump($this->tablegateway->getlastinsertvalue());die; return $id; } else { if ($this->findone($id)) { $this->tablegateway->update($data, array('id' => $id)); } else { throw new \exception('form id not exist'); } } }
and $row return int(1) always, $this->tablegateway->getlastinsertvalue() returns null. doing wrong? i've tried everything... no luck.
you can try raw query see if getting result want.
$adapter = $this->tablegateway->getadapter();
$rowset = "select max(id)..."; $resultset = $adapter->query($rowset,array()); $rowdata = $resultset->toarray(); return $rowdata;
or
use zend\db\sql\sql; use zend\db\sql\select; use zend\db\sql\expression; $adapter = $this->tablegateway->getadapter(); $sql = new sql($adapter); $select = $sql->select(); $select->from('table_name')->columns(array('id' => new expression('max(id)') )); $selectstring = $sql->getsqlstringforsqlobject($select); $results = $adapter->query($selectstring, $adapter::query_mode_execute); $resultset = new resultset(); $resultset->initialize($results); return $resultset->toarray();
Comments
Post a Comment