aliobeidat / yii2-oci8
使用编写良好的pdo-via-oci8包的Yii2扩展,用于标准PHP Oracle函数(OCI8)
dev-master
2022-11-29 06:15 UTC
Requires
- php: ^8.1
- yajra/laravel-pdo-via-oci8: 3.*
This package is auto-updated.
Last update: 2024-09-29 06:36:08 UTC
README
Yii2 OCI8扩展,使用yajra/pdo-via-oci8库,可选完整表架构缓存。支持PHP8。
支持
- Yii 2.x;
- yajra/pdo-via-oci8 3.x;
- >= PHP 8.1
安装
将以下内容添加到您的 composer.json
文件中
"require": {
"aliobeidat/yii2-oci8": "dev-master"
}
然后运行 composer update
。
Oracle数据库的Yii2配置示例
Yii2配置
$config = [ ... 'components' => [ ... 'db' => require(__DIR__ . '/db.php'), ... ] ];
在 db.php
中配置数据库
return [ 'class' => 'aliobeidat\yii2oci8\Oci8Connection', 'dsn' => 'oci:dbname=//192.168.0.1:1521/db.local;charset=AL32UTF8;', 'username' => 'user', 'password' => 'pass', 'attributes' => [ PDO::ATTR_PERSISTENT => true ], 'enableSchemaCache' => true, //Oracle dictionaries is too slow :(, enable caching 'schemaCacheDuration' => 60 * 60, //1 hour 'on afterOpen' => function($event) { /* A session configuration example */ $q = <<<SQL begin execute immediate 'alter session set NLS_SORT=BINARY_CI'; execute immediate 'alter session set NLS_TERRITORY=AMERICA'; end; SQL; $event->sender->createCommand($q)->execute(); } ];
示例
请随意使用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
或 main.php
中添加以下行
... //Disabling Yii2 schema caching 'enableSchemaCache' => false //Defining a cache schema component 'cachedSchema' => [ 'class' => 'aliobeidat\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 everythings but the EMP table from HR and SCOTT schemas return $tableName != 'EMP'; } ], ...
将表架构保存到默认的Yii2缓存组件。在连接打开后构建架构缓存
'on afterOpen' => function($event) { $event->sender->createCommand($q)->execute(); /* @var $schema \aliobeidat\yii2oci8\CachedSchema */ $schema = $event->sender->getSchema(); if (!$schema->isCached) //Rebuild schema cache $schema->buildSchemaCache(); },