ecphp/doctrine-oci8

支持游标的Doctrine OCI8 (Oracle) 驱动程序。

1.0.0 2021-02-17 15:06 UTC

README

Latest Stable Version GitHub stars Total Downloads GitHub Workflow Status Scrutinizer code quality Type Coverage Code Coverage Read the Docs License

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_STMTOCI8::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。

要运行测试,请执行以下步骤

  1. cp .env.example .env
  2. docker-compose up -d
  3. docker-compose exec php ./vendor/bin/phpunit
  4. CTRL+C
  5. docker-compose down