Class yii\db\mssql\DBLibPDO
| Inheritance | yii\ |
|---|---|
| Available since version | 2.0.41 |
| Source Code | https://github.com/yiisoft/yii2/blob/master/framework/db/mssql/DBLibPDO.php |
This is an extension of the default PDO class of DBLIB drivers.
It provides workarounds for improperly implemented functionalities of the DBLIB drivers.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| getAttribute() | Retrieve a database connection attribute. | yii\ |
| lastInsertId() | Returns value of the last inserted ID. | yii\ |
Method Details
Retrieve a database connection attribute.
It is necessary to override PDO's method as some MSSQL PDO driver (for example, dblib) does not support getting attributes.
| public mixed getAttribute ( integer $attribute ) | ||
| $attribute | integer |
One of the PDO::ATTR_* constants. |
| return | mixed |
A successful call returns the value of the requested PDO attribute. An unsuccessful call returns
|
|---|---|---|
public function getAttribute($attribute): mixed
{
$sql = <<<SQL
SELECT CAST(SERVERPROPERTY('productversion') AS VARCHAR)
SQL;
try {
return parent::getAttribute($attribute);
} catch (PDOException $e) {
switch ($attribute) {
case self::ATTR_SERVER_VERSION:
// Reached only on the `dblib` driver (throws on `getAttribute()`); never on `sqlsrv`/CI.
// @codeCoverageIgnoreStart
return $this->query($sql)->fetchColumn();
// @codeCoverageIgnoreEnd
default:
throw $e;
}
}
}
Returns value of the last inserted ID.
| public string|false lastInsertId ( string|null $name = null ) | ||
| $name | string|null |
The sequence name. Defaults to null. |
| return | string|false |
Last inserted ID value. |
|---|---|---|
public function lastInsertId(string|null $name = null): string|false
{
$sql = <<<SQL
SELECT CAST(COALESCE(SCOPE_IDENTITY(), @@IDENTITY) AS bigint)
SQL;
$id = $this->query($sql)->fetchColumn();
return $id === null ? false : (string) $id;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.