3.2 - 严重 - 未处理
PHP函数中PDO::lastInsertId() ([ string $name ] ),
Name参数对于PostgreSQL是必需的,需要传入序列的名称(序列名与表名在名字上可能会没有任何联系,需要手动指定)。
而在ThinkPHP的Pdo.class.php中的函数getLastInsertId()中,没有针对PostgreSQL做处理。这导致程序将无法获取lastInsertId。
解决方案:
ThinkPHP\Library\Think\Model.class.php中添加函数
/**
* 设置自增序列名
* @param string $name
* return \Think\Model
*/
public function setSequence($name){
$this->db->setSequenceName($name);
return $this;
}
/**
* 获取自增序列名
* @param string $name
*/
public function getSequence($name){
return $this->db->getSequenceName();
}修改ThinkPHP\Library\Think\Db\Driver\Pdo.class.php1. 添加私有属性
private $sequenceName = '';2. 增加函数 /**
* 设置自增序列名
* @param string $name
*/
public function setSequenceName($name){
$this->sequenceName = $name;
}
/**
* 获取自增序列名
* @param string $name
*/
public function getSequenceName(){
return $this->sequenceName;
}3. 修改函数getLastInsertId() public function getLastInsertId() {
switch($this->dbType) {
//---------------------------------------------------------------
case 'PGSQL':
$sequenceName = $this->getSequenceName();
return $this->_linkID->lastInsertId($sequenceName);
//---------------------------------------------------------------
case 'SQLITE':
case 'MSSQL':
case 'SQLSRV':
case 'IBASE':
case 'MYSQL':
return $this->_linkID->lastInsertId();
case 'ORACLE':
case 'OCI':
$sequenceName = $this->table;
$vo = $this->query("SELECT {$sequenceName}.currval currval FROM dual");
return $vo?$vo[0]["currval"]:0;
}
}修改函数Execute()public function execute($str, $bind = array()) {
$this->initConnect ( true );
if (! $this->_linkID)
return false;
$this->queryStr = $str;
if (! empty ( $bind )) {
$this->queryStr .= '[ ' . print_r ( $bind, true ) . ' ]';
}
$flag = false;
//---------------------------------------------------------------
if ($this->dbType == 'OCI' || $this->dbType == 'PGSQL') {
if (preg_match ( "/^\s*(INSERT\s+INTO)\s+(\w+)\s+/i", $this->queryStr, $match )) {
$this->table = C ( "DB_SEQUENCE_PREFIX" ) . str_ireplace ( C ( "DB_PREFIX" ), "", $match [2] );
if ($this->dbType == 'OCI') {
$flag = ( boolean ) $this->query ( "SELECT * FROM user_sequences WHERE sequence_name='" . strtoupper ( $this->table ) . "'" );
} else if ($this->dbType == 'PGSQL') {
$sequenceName = $this->getSequenceName ();
if (empty ( $sequenceName )) {
// PostgreSQL里,创建表时,如果使用serial类型,默认生成的自增序列名为:'表名' + '_' + '字段名' + '_' + 'seq'
$sequenceName = $this->table . '_id_seq';
$this->setSequenceName ( $sequenceName );
}
/*
* @TODO
* 此句会导致 M()->getLastSql()返回下面的select语句,临时打为true
* $flag = (boolean)$this->query('SELECT * FROM ' .$sequenceName);
*/
}
}
}
//---------------------------------------------------------------
// 释放前次的查询结果
if ( !empty($this->PDOStatement) ) $this->free();
...
...
...这样,对于bookmarks表,对应自增序列名为bookmarks_id_seq,就可以获取自增的ID $m = M('bookmarks');
$data = array (
'book_id' => rand ( 0, 9999999 ),
'member_id' => rand ( 0, 9999999 )
);
$m->add ($data);
dump($m->getLastInsId());而对于books_type1,books_type2表,两表主键book_id,均使用自增序列books,也可以获取自增ID $m = M('books_type1')->setSequence('books');
$data = array (
'amount' => rand ( 0, 9999999 )
);
$m->add ($data);
dump($m->getLastInsId());
// 或
$m = M('books_type2');
$data = array (
'amount' => rand ( 0, 9999999 )
);
$m->setSequence('books')->add ($data);
dump($m->getLastInsId()); 