Yii2将文件保存到Oracle BLOB [英] Yii2 saving file to Oracle BLOB
问题描述
问题是我无法将文件保存到Blob.它的工作原理没有任何错误,临时文件已创建,我可以从中读取.我检查了它是否要绑定-是的,它具有正确的资源值和 \ PDO :: PARAM_LOB
数据类型.
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.
我有一个ActiveRecord类:
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);
}
}
表格:
CREATE TABLE NEWS
(
RN NUMBER(17,0) PRIMARY KEY NOT NULL,
IMAGE BLOB
);
显示此查询的日志:
INSERT INTO "NEWS" ("IMAGE") VALUES (:qp4) RETURNING "RN" INTO :qp8
那么它实际上并没有绑定它或什么?
So it didn't actually bind it or what?
推荐答案
PDO_OCI似乎正在以旧式方式与Oracle一起使用,因此您需要首先开始事务插入 EMPTY_BLOB()
,然后将变量与指向文件的指针绑定,执行语句并提交.
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.
我已经完成了更新,因为我不想手动进行完整查询.首先,我将指向文件的指针写入 save()
方法的 $ this-> file
中,并调用 $ model-> save&&$ model-> saveImage()
在控制器中.
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();
}
这篇关于Yii2将文件保存到Oracle BLOB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!