Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php ZEND2-lastInsertValue返回NULL_Php_Postgresql_Zend Framework2 - Fatal编程技术网

Php ZEND2-lastInsertValue返回NULL

Php ZEND2-lastInsertValue返回NULL,php,postgresql,zend-framework2,Php,Postgresql,Zend Framework2,我的桌子看起来像这样: CREATE TABLE data.brand_list ( id bigserial NOT NULL, brand_name text, created_at timestamp with time zone DEFAULT now(), ghost boolean, CONSTRAINT brand_list_id_pkey PRIMARY KEY (id) ) 它还具有以下顺序: CREATE SEQUENCE data.brand_lis

我的桌子看起来像这样:

CREATE TABLE data.brand_list
(
  id bigserial NOT NULL,
  brand_name text,
  created_at timestamp with time zone DEFAULT now(),
  ghost boolean,
  CONSTRAINT brand_list_id_pkey PRIMARY KEY (id)
)
它还具有以下顺序:

 CREATE SEQUENCE data.brand_list_id_seq
 INCREMENT 1
 MINVALUE 1
 MAXVALUE 9223372036854775807
 START 649
 CACHE 1;
 ALTER TABLE data.brand_list_id_seq
我在Zend模型中的代码:

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 does not exist');
        }
    }
}

$row return int(1)always,$this->tableGateway->getLastInsertValue()返回NULL。我做错了什么?我什么都试过了。。。没有运气

嗯,我找到了可能不太优雅的解决方案,但它对我很有效。我发现-Zend 2在postgres序列方面存在问题。这是我的解决方案:

$sql = "SELECT nextval('data.brand_list_id_seq') nextval";
        $resultSet = $this->tableGateway->getAdapter()->getDriver()->getConnection()->execute($sql);
        foreach ($resultSet as $item) {
            $data['id'] = $item['nextval'];
        }
        $this->tableGateway->insert($data);

        return $data['id'];

您应该将上面的代码应用于模型插入方法。

尝试
$this->tableGateway->lastInsertValue

而不是
$this->tableGateway->lastInsertValue()

您可以尝试一个原始查询,看看您是否得到了想要的结果


我也尝试过这种方法,得到NULL,因为$this->tableGateway->lastInsertValue();返回受保护的$this->tableGateway->lastInsertValue;
  $adapter = $this->tableGateway->getAdapter();
$rowset = "SELECT MAX(ID)...";

$resultset = $adapter->query($rowset,array());

$rowData = $resultset->toArray();
return $rowData;
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();