ホームページ  >  記事  >  バックエンド開発  >  CI フレームワークでよく使用される関数のカプセル化について

CI フレームワークでよく使用される関数のカプセル化について

不言
不言オリジナル
2018-06-14 13:50:551927ブラウズ

この記事では、主に CI フレームワークの共通関数のカプセル化を紹介し、CI フレームワークの一般的に使用されるデータベースのクエリ、挿入、削除、更新、デバッグ操作のスキルを例の形式で分析します。 it

この記事の例では、CI フレームワークの共通関数のカプセル化について説明します。参考のために皆さんと共有してください。詳細は次のとおりです。

/**
* 封装查询函数
*/
public function get_what($table='',$where=array(),$fields = ' * '){
    if( '' == $table ){
      return false;
    }
    //查询并返回相关结果
    $query = $this->db->select($fields)->where($where)->get($table);
    $res = $query->result_array();
    return $res;
}
/**
* 封装单条查询函数
*/
public function get_row($table='',$where=array(),$fields = ' * '){
    if( '' == $table ){
      return false;
    }
    //查询并返回相关结果
    $query = $this->db->select($fields)->where($where)->get($table);
    $res = $query->row_array();
    return $res;
}
/**
* 封装更新函数
*/
public function update_what($table='', $where=array(), $data = array()){
    if('' == $table || true === empty($where) || true === empty($data)){
      return false;
    }
    //更新相应的字段
    $query = $this->db->update($table,$data,$where);
    return $query;
}
/**
* 扩展数据库函数之自增 自减
* using:
* $table = 'codeuser';
$where = array('id'=>1);
$data = array('usestate'=>'usestate+1','imgtype' => 'imgtype-1');
*/
public function update_count($table = '', $where=array(), $data=array()){
     //如果表名为空 或者数据为空则直接 返回false
     if('' == $table || empty($data)){
       return false;
     }
     foreach($data as $key => $val){
       if(false !== stripos($val,'+') || false !== stripos($val,'-')){
         $this->db->set($key, $val, FALSE);
       }else{
         $this->db->set($key, $val);
       }
     }
     $res = $this->db->where($where)->update($table);
     return $res;
}
/**
* 封装插入函数
*/
public function insert_what($table = '', $data = array()){
    if('' == $table || true === empty($data)){
      return false;
    }
    //插入 相关记录
    $query = $this->db->insert($table, $data);
    return $query;
}
/**
* 删除记录封装函数
*/
public function delete_what($table = '', $where=array()){
    if(true === empty($where) || '' == $table){
      return false;
    }
    //删除相关表记录
    $query = $this->db->delete($table,$where);
    return $query;
}
/**
* debug 相关函数
*/
 public function debug_what($org_error = ''){
    $con = $this->router->fetch_class();
    $func = $this->router->fetch_method();
    if($org_error){
      $error .= date("Y-m-d H:i:s",time())."\r\n";
      $error .= __FILE__."\r\n";
      $error .= $con." 控制器下的:\r\n";
      $error .= $func." 方法调试信息如下:\r\n";
      $error .= $org_error;file_put_contents("./error_log.txt",$error."\r\n",FILE_APPEND);
    }
}

上記がこの記事の全内容です。皆さんの学習に役立つことを願っています。その他の関連コンテンツについては、PHP 中国語 Web サイトに注目してください。

関連する推奨事項:

一般的に使用される画像処理方法の CI フレームワークのカプセル化について

Redis を操作する CI フレームワーク方法について

## システムコアクラスを拡張するCIフレームワークの手法に関する分析

以上がCI フレームワークでよく使用される関数のカプセル化についての詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。