在CodeIgniter中插入Oracle CLOB

时间:2012-06-13 06:20:46

标签: php oracle codeigniter clob

我们如何使用CodeIgniter Framework插入到具有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));

但这似乎不起作用。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

CodeIgniter不支持此功能,您需要手动执行此操作。为了不重新创建与数据库的连接,您可以使用CI创建的连接ID:

$this->db->conn_id

答案 1 :(得分:0)

我解决了它在模型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;
}

公共函数updateClobImg64($ file_pk1,$ img64){

    $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>

希望这对解决这一问题有所帮助。

相关问题