PHP 连接 Oracle

起因

由于项目的数据库需要用客户购买的Oracle数据库,所以需要php安装oci扩展。

运行环境

php : 7.2

系统: windows10

oracle: 11gR2

安装相关环境

由于php的oci8扩展还是需要使用到oracle的一些包,所以先下载这一些。

PHP连接Oracle

下载完成后解压缩这个压缩包,并且将这个包的路径添加到PATH中。

PHP连接Oracle

下载php的oci8扩展

windows可以直接到这个网址上下载相应的dll,pecl oci8

如果是安装了pecl的话,可以直接运行 pecl install oci8

下载完成后放到php的ext目录下,并且编辑php.ini文件,添加extension=php_oci8.dll

这样就完成了php与oracle的配置了。

在Laravel中使用

Laravel默认支持的数据库不包含oracle,可以使用 yajra/laravel-oci8这一个包来让Laravel支持oracle

composer require yajra/laravel-oci8

由于这个包已经支持Laravel的自动加载,也就不需要自己手动去注册了

    "extra": {
        "branch-alias": {
          "dev-master": "5.6-dev"
        },
        "laravel": {
            "providers": [
                "Yajra\\Oci8\\Oci8ServiceProvider"
            ]
        }
    },

这样就可以使用php连接到oracle了。

一个坑

我们在使用migration来管理表格的时候,一般情况下会对每一个表都要有注释(为了别人能看得懂。。。)。然而这个包有一个bug,就是对于表格的注释缺少了前缀。

表格的注释是成员变量而不是方法噢。

   /**
     * Run the comment on table statement.
     * Comment set by $table->comment = 'comment';.
     *
     * @param \Yajra\Oci8\Schema\OracleBlueprint $blueprint
     */
    private function commentTable(OracleBlueprint $blueprint)
    {
        $table = $this->wrapValue($blueprint->getTable());

        if ($blueprint->comment != null) {
            $this->connection->statement("comment on table {$table} is '{$blueprint->comment}'");
        }
    }

这一段代码在/vendor/yajar/laravel-oci8/src/Oci8/Schema/Comment.php 中第40行。

这里少了对表前缀的引用,导致我们在migrate的时候生成的sql是缺少了表前缀的,所以在这里添加一个表前缀上去解决这个问题

    /**
     * Run the comment on table statement.
     * Comment set by $table->comment = 'comment';.
     *
     * @param \Yajra\Oci8\Schema\OracleBlueprint $blueprint
     */
    private function commentTable(OracleBlueprint $blueprint)
    {
        $table = $this->wrapValue($blueprint->getTable());

        if ($blueprint->comment != null) {
            $this->connection->statement("comment on table {$this->connection->getTablePrefix()}{$table} is '{$blueprint->comment}'");
        }
    }
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!