pdynarowski / yii2-oci8
基于yajra/laravel-pdo-via-oci8的Yii2扩展
2.0.2
2021-03-06 21:11 UTC
Requires
- php: ^8.0
- yajra/laravel-pdo-via-oci8: ^3.0.0
This package is auto-updated.
Last update: 2024-09-07 05:18:02 UTC
README
此扩展是基于Neconix/Yii2-Oci8进行分支,以支持较新版本的yajra/pdo-via-oci8和PHP 8的Yii2 OCI8扩展。该扩展使用编写良好的pdynarowski/pdo-via-oci8,并可选择启用完整表模式缓存。支持的PHP版本为7.4和8。
支持
- Yii 2.x
- yajra/pdo-via-oci8 2.x
- >= PHP 7.4
- >= PHP 8
安装
将以下内容添加到您的composer.json
文件中
"require": {
"pdynarowski/yii2-oci8": "^1.1"
}
然后运行composer update
。
Oracle数据库的Yii2配置示例
Yii2配置
$config = [ ... 'components' => [ ... 'db' => require(__DIR__ . '/db.php'), ... ] ];
db.php
中的数据库配置
return [ 'class' => 'pdynarowski\yii2oci8\Oci8Connection', 'dsn' => 'oci:dbname=//192.168.0.1:1521/instance_name;charset=UTF8', 'username' => 'username', 'password' => 'password', 'enableSchemaCache' => false, 'on afterOpen' => function($event) { $event->sender->createCommand("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'")->execute(); }, 'schemaCacheDuration' => 0, 'attributes' => [ PDO::ATTR_PERSISTENT => true ], 'on afterOpen' => function($event) { /* @var $schema \neconix\yii2oci8\CachedSchema */ $event->sender->createCommand("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'")->execute(); $schema = $event->sender->getSchema(); $schema->schemaCacheDuration = 0; // 0 - never expire if (!$schema->isCached) { //Rebuild schema cache $schema->buildSchemaCache(); } }, //Defining a cache schema component 'cachedSchema' => [ 'class' => 'pdynarowski\yii2oci8\CachedSchema', 'schemaCacheDuration'=>0, //60*60, ], ];
示例
请随意使用Yii2的ActiveRecord
方法
$cars = Car::find()->where(['YEAR' => '1939'])->indexBy('ID')->all();
获取原始数据库处理器并与之交互
$dbh = Yii::$app->db->getDbh(); $stmt = oci_parse($dbh, "select * from DEPARTMENTS where NAME = :name"); $name = 'NYPD'; oci_bind_by_name($stmt, ':name', $name); oci_execute($stmt); ... //fetching result
缓存功能
要在模式中的所有表中启用缓存,请在数据库连接配置db.php
中添加以下行
... //Disabling Yii2 schema cache 'enableSchemaCache' => false //Defining a cache schema component 'cachedSchema' => [ 'class' => 'pdynarowski\yii2oci8\CachedSchema', // Optional, default is the current connection schema. 'cachingSchemas' => ['HR', 'SCOTT'], // Optional. This callback must return `true` for a table name if it need to be cached. 'tableNameFilter' => function ($tableName, $schemaName) { //Cache everything but the EMP table from HR and SCOTT schemas return $tableName != 'EMP'; } ], ...
表模式保存到默认的Yii2缓存组件。在连接打开后构建模式缓存
'on afterOpen' => function($event) { $event->sender->createCommand($q)->execute(); /* @var $schema \pdynarowski\yii2oci8\CachedSchema */ $schema = $event->sender->getSchema(); if (!$schema->isCached) //Rebuild schema cache $schema->buildSchemaCache(); },