如何使用CodeIgniter框架插入到具有CLOB列的表中?
表是:
CREATE TABLE NOTIFICATIONS
{
ID NUMBER,
CONTENT CLOB
}
PHP代码是:
$id = 1;
$content = /* An Incredibly Long & Complex String */
$query = "INSERT INTO notifications (id,content) values (?,?)";
$this->dbconnxn->query($query, array($id,$content));
但这似乎行不通。
有什么想法吗?
CodeIgniter 不支持此功能,您需要手动执行此操作。为了不重新创建与数据库的连接,可以使用 CI 创建的连接 ID:
$this->db->conn_id
我已经解决了它,在模型 2 中创建函数 1) 用于插入和更新 clob 和 2) 用于读取 clob:
1)在模型中,我为设置Clob而创建的函数是updateClobImg64,它在插入后调用:
公共函数 create_file($file){
$file->file_pk1=$this->getMaxPk1();
$data = array(
"file_pk1" => $file->file_pk1,
"file_name" => $file->file_name,
"file_type"=>$file->file_type,
"file_location"=>$file->file_location,
//"file_img64"=>$file->file_img64,
"file_created_date"=>$file->file_created_date,
"file_created_by" => $file->file_created_by,
"file_storage_type"=>$file->file_storage_type,
);
$this->db->insert("file_repository", $data);
if(isset($file->file_img64) && !empty($file->file_img64)) $this->updateClobImg64($file->file_pk1,$file->file_img64);
return $file->file_pk1;
}
public function updateClobImg64($file_pk1,$img 64){
$sql='update "file_repository" set "file_img64"=EMPTY_CLOB() where "file_pk1"='.$file_pk1.' RETURNING "file_img64" INTO :clob';
$stid = oci_parse($this->db->conn_id, $sql);
$clob = oci_new_descriptor($this->db->conn_id, OCI_D_LOB);
oci_bind_by_name($stid, ":clob", $clob, -1, OCI_B_CLOB);
oci_execute($stid, OCI_NO_AUTO_COMMIT); // use OCI_DEFAULT for PHP <= 5.3.1
$clob->save($img64);
oci_commit($this->db->conn_id);
$clob->free();
OCIFreeStatement($stid);
}</span>
2)要读取clob需要一个我以这种方式实现的函数读取: function read_clob($field) {
return $field->read($field->size());
}
和 在模型选择函数中调用:
公共函数 get_all(){
$query = $this->db->get_where("file_repository", array());
if($query->num_rows() > 0){
$files=$query->result();
foreach($files as $key=>$row){
$files[$key]->file_img64=$this->read_clob($row->file_img64);
}
return $files;
}
}</span>
希望这对解决这个问题有所帮助。