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 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler