ecphp / doctrine-oci8
支持游标的Doctrine OCI8 (Oracle) 驱动程序。
1.0.0
2021-02-17 15:06 UTC
Requires
- php: >= 7.4
- ext-oci8: *
- doctrine/dbal: ^2.6
Requires (Dev)
- drupol/php-conventions: ^3.0
- phpunit/phpunit: ^9
- symfony/dotenv: ^5
This package is auto-updated.
Last update: 2024-09-21 13:32:39 UTC
README
Doctrine OCI8 驱动程序
支持游标的Doctrine OCI8 驱动程序,适用于PHP >= 7.4。
这是从Jason Hofer的原包develpup/doctrine-oci8-extended派生出来的。
安装
composer require ecphp/doctrine-oci8
配置
Symfony 5
使用ecphp/doctrine-oci8-bundle来自动配置参数。
如果您更喜欢修改配置,请按如下方式编辑doctrine.yaml
doctrine: dbal: driver_class: EcPhp\DoctrineOci8\Doctrine\DBAL\Driver\OCI8\Driver types: cursor: EcPhp\DoctrineOci8\Doctrine\DBAL\Types\CursorType
使用
<?php namespace App; use Doctrine\DBAL\Types\Type; use EcPhp\DoctrineOci8\Doctrine\DBAL\Types\CursorType; use EcPhp\DoctrineOci8\Doctrine\DBAL\Driver\OCI8\Driver; include __DIR__ .'/vendor/autoload.php'; // Register the custom type. if (false === Type::hasType('cursor')) { Type::addType('cursor', CursorType::class); } $config = new Doctrine\DBAL\Configuration(); $params = [ 'dbname' => 'database_sid', 'user' => 'database_username', 'password' => 'database_password', 'host' => 'database.host', 'port' => 1521, 'persistent' => true, 'driverClass' => Driver::class, // This is where we load the driver. ]; $conn = Doctrine\DBAL\DriverManager::getConnection($params, $config); $stmt = $conn->prepare('BEGIN MY_STORED_PROCEDURE(:user_id, :cursor); END;'); $stmt->bindValue('user_id', 42); $stmt->bindParam('cursor', $cursor, \PDO::PARAM_STMT); $stmt->execute(); /** @var $cursor EcPhp\DoctrineOci8\Doctrine\DBAL\Driver\OCI8\OCI8Cursor */ $cursor->execute(); while ($row = $cursor->fetch()) { print_r($row); echo PHP_EOL; } $cursor->closeCursor(); $stmt->closeCursor();
类型
对于不通过PDO::PARAM_
常量表示的OCI8
类型,将OCI8::PARAM_
常量作为bindValue()
和bindParam()
的type
参数传递。
游标
游标可以指定为PDO::PARAM_STMT
、OCI8::PARAM_CURSOR
或简单的'cursor'
。只能使用bindParam()
方法将游标绑定到语句。
子游标
结果集列中返回的游标资源将自动获取。您可以通过传递以下之一来更改此行为
OCI8::RETURN_RESOURCES
返回原始PHP资源。OCI8::RETURN_CURSORS
返回尚未执行的OCI8Cursor
对象。
use Doctrine\DBAL\Driver\OCI8\OCI8; $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC+OCI8::RETURN_CURSORS); $rows = $stmt->fetchAll(\PDO::FETCH_BOTH+OCI8::RETURN_RESOURCES);
特别感谢Michal Tichý的贡献。
测试
为了有一个工作的开发环境,测试基于Docker。
要运行测试,请执行以下步骤
cp .env.example .env
docker-compose up -d
docker-compose exec php ./vendor/bin/phpunit
CTRL+C
docker-compose down