dongmeng4742 2015-09-11 11:29
浏览 65
已采纳

Yii2将文件保存到Oracle BLOB

Problem is that i can't save file to blob. It works without any error, temp file is created and i can read from it. I checked if it goes to bind - yes it goes with right resource value and with \PDO::PARAM_LOB data type.

I have an ActiveRecord class:

class News extends ActiveRecord
{
    public function rules()
    {
        return [
            [
                ['image'],
                'image',
                'extensions' => 'png jpg',
                'maxSize' => 1024 * 300,
            ]
        ];
    }

    public function beforeSave($insert)
    {
        $fileInfo = UploadedFile::getInstance($this, 'image');
        $this->image = fopen($fileInfo->tempName, 'r+');
        return parent::beforeSave($insert);
    }

}

Table:

CREATE TABLE NEWS
(
    RN NUMBER(17,0) PRIMARY KEY NOT NULL,
    IMAGE BLOB
);

Logs showing this query:

INSERT INTO "NEWS" ("IMAGE") VALUES (:qp4) RETURNING "RN" INTO :qp8

So it didn't actually bind it or what?

  • 写回答

2条回答 默认 最新

  • dongpinken0498 2015-09-14 15:01
    关注

    It appears that PDO_OCI is working with Oracle in old-style way, so you need to start a transaction insert EMPTY_BLOB() first, then bind variable with pointer to file, execute statement and commit.

    I've done it with update, because i don't want to make full query manually. First i write pointer to file into $this->file in save() method and call $model->save && $model->saveImage() in controller.

    public function saveImage()
    {
        if (!$this->file) {
            return true;
        }
        $table = self::tableName();
        $rn = $this->rn;
        $command = Yii::$app->getDb()->createCommand(
            "UPDATE $table SET IMAGE=EMPTY_BLOB() WHERE RN=:rn RETURNING IMAGE INTO :image"
        );
        $command->bindParam(':rn', $rn, \PDO::PARAM_STR);
        $command->prepare();
        $command->bindParam(':image', $this->file, \PDO::PARAM_LOB);
        return $command->execute();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 vue2(标签-chrome|关键词-浏览器兼容)
  • ¥15 python网络流自动生成系统 医学领域
  • ¥15 代码的修改,添加和运行完善
  • ¥15 krpano-场景分组和自定义地图分组
  • ¥15 lammps Gpu加速出错
  • ¥15 关于PLUS模型中kapaa值的问题
  • ¥15 关于博途V17进行仿真时无法建立连接问题
  • ¥15 机器学习教材中的例题询问
  • ¥15 求.net core 几款免费的pdf编辑器
  • ¥15 为什么安装HCL 和virtualbox之后没有找到VirtualBoxHost-OnlyNetWork?