diff --git a/Cake/Database/Connection.php b/Cake/Database/Connection.php index f145f014251..0d1220b53f4 100644 --- a/Cake/Database/Connection.php +++ b/Cake/Database/Connection.php @@ -470,16 +470,6 @@ public function quoteIdentifier($identifier) { return $this->_driver->quoteIdentifier($identifier); } -/** - * Returns last id generated for a table or sequence in database - * - * @param string $table table name or sequence to get last insert value from - * @return string|integer - */ - public function lastInsertId($table) { - return $this->_driver->lastInsertId($table); - } - /** * Enables or disables query logging for this connection. * diff --git a/Cake/Database/Driver.php b/Cake/Database/Driver.php index 962d53a92fc..045eed09c11 100644 --- a/Cake/Database/Driver.php +++ b/Cake/Database/Driver.php @@ -164,10 +164,11 @@ public function schemaValue($value) { * Returns last id generated for a table or sequence in database * * @param string $table table name or sequence to get last insert value from + * @param string column the name of the column representing the primary key * @return string|integer */ - public function lastInsertId($table = null) { - return $this->_connection->lastInsertId($table); + public function lastInsertId($table = null, $column = null) { + return $this->_connection->lastInsertId($table, $column); } /** diff --git a/Cake/Database/Driver/PDODriverTrait.php b/Cake/Database/Driver/PDODriverTrait.php index 6985ec5b4f4..4ea571a5dec 100644 --- a/Cake/Database/Driver/PDODriverTrait.php +++ b/Cake/Database/Driver/PDODriverTrait.php @@ -126,9 +126,9 @@ public function quote($value, $type) { * @param string $table table name or sequence to get last insert value from * @return string|integer */ - public function lastInsertId($table = null) { + public function lastInsertId($table = null, $column = null) { $this->connect(); - return $this->_connection->lastInsertId(); + return $this->_connection->lastInsertId($table); } /** diff --git a/Cake/Database/Statement/StatementDecorator.php b/Cake/Database/Statement/StatementDecorator.php index 0570797fad5..40147bea65f 100644 --- a/Cake/Database/Statement/StatementDecorator.php +++ b/Cake/Database/Statement/StatementDecorator.php @@ -264,4 +264,18 @@ public function bind($params, $types) { } } +/** + * Returns the latest primary inserted using this statement + * + * @param string $table table name or sequence to get last insert value from + * @param string column the name of the column representing the primary key + * @return string + */ + public function lastInsertId($table = null, $column = null) { + if ($column && $row = $this->fetch('assoc')) { + return $row[$column]; + } + return $this->_driver->lastInsertId($table, $column); + } + } diff --git a/Cake/Database/StatementInterface.php b/Cake/Database/StatementInterface.php index 1b410b058c9..83cf158da54 100644 --- a/Cake/Database/StatementInterface.php +++ b/Cake/Database/StatementInterface.php @@ -159,4 +159,13 @@ public function count(); */ public function bind($params, $types); +/** + * Returns the latest primary inserted using this statement + * + * @param string $table table name or sequence to get last insert value from + * @param string column the name of the column representing the primary key + * @return string + */ + public function lastInsertId($table = null, $column = null); + } diff --git a/Cake/Test/TestCase/Database/Statement/StatementDecoratorTest.php b/Cake/Test/TestCase/Database/Statement/StatementDecoratorTest.php new file mode 100644 index 00000000000..cd9717eb24f --- /dev/null +++ b/Cake/Test/TestCase/Database/Statement/StatementDecoratorTest.php @@ -0,0 +1,63 @@ +getMock('\PDOStatement'); + $driver = $this->getMock('\Cake\Database\Driver'); + $statement = new StatementDecorator($statement, $driver); + + $driver->expects($this->once())->method('lastInsertId') + ->with('users') + ->will($this->returnValue(2)); + $this->assertEquals(2, $statement->lastInsertId('users')); + } + +/** + * Tests that calling lastInsertId will get the + * + * @return void + */ + public function testLastInsertIdWithReturning() { + $internal = $this->getMock('\PDOStatement'); + $driver = $this->getMock('\Cake\Database\Driver'); + $statement = new StatementDecorator($internal, $driver); + + $internal->expects($this->once())->method('fetch') + ->with('assoc') + ->will($this->returnValue(['id' => 2])); + $driver->expects($this->never())->method('lastInsertId'); + $this->assertEquals(2, $statement->lastInsertId('users', 'id')); + } + +}