diff --git a/lib/Cake/Model/Datasource/DboSource.php b/lib/Cake/Model/Datasource/DboSource.php index 6c99a1df1..6ca24960f 100644 --- a/lib/Cake/Model/Datasource/DboSource.php +++ b/lib/Cake/Model/Datasource/DboSource.php @@ -460,7 +460,11 @@ class DboSource extends DataSource { * @return string Error message with error number */ public function lastError(PDOStatement $query = null) { - $error = $query->errorInfo(); + if ($query) { + $error = $query->errorInfo(); + } else { + $error = $this->_connection->errorInfo(); + } if (empty($error[2])) { return null; } diff --git a/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php b/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php index 4e644545f..13765845c 100644 --- a/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php @@ -39,6 +39,10 @@ class DboTestSource extends DboSource { public function setConfig($config) { $this->config = $config; } + + public function setConnection($conn) { + $this->_connection = $conn; + } } /** @@ -786,4 +790,20 @@ class DboSourceTest extends CakeTestCase { $this->assertEqual(' GROUP BY created', $result); } +/** + * Test getting the last error. + */ + function testLastError() { + $result = $this->db->lastError(); + $this->assertNull($result); + + $stmt = $this->getMock('PDOStatement'); + $stmt->expects($this->any()) + ->method('errorInfo') + ->will($this->returnValue(array('', 'something', 'bad'))); + + $result = $this->db->lastError($stmt); + $expected = 'something: bad'; + $this->assertEquals($expected, $result); + } }