기본 콘텐츠로 건너뛰기

CodeIgniter Straight Model

CodeIgniter Straight Model

Blue Breeze

푸른바람 C/H 2014. 12. 9. 13:32

코드이그나이트 스트라이트 모델

코드이그나이트에서 공용으로 사용할 수 있는 스트라이트 모델입니다.

개발을 하면서 많은 기능이 들어간 모델을 만들었는데 결국 간단하고 어디에서든지 사용가능한 모델이 편하더군요.

데이터베이스 테이블 기본 조건과 함께 사용설명이 있으니 쉽게 사용 가능합니다.

좀 더 복잡한 모델은 Case by Case 직접 수정하시면 됩니다. ^^

테이블 포멧

idx INT, signed, NOT NULL, Primary Key, AUTO_INCREMENT, 기본키 zidx INT, signed, NULL, 정렬 기본값은 idx*-1 ids Varchar, AnySize, Primary Key, 사용자 정의 기본키 사용시 created_ip INT, unsigned, NULL, 생성 IP created timestamp, NULL, 생성시간 updated timestamp, NOT NULL, CURRENT_TIMESTAMP, ON UPDATE CURRENT_TIMESTAMP, 수정시간 Other Fields Custom Definition, 사용자 정의

load->mode('straight_model', 'straight'); * $this->straight->setTable('tableName'); // set TableName, default 'user' * $this->straight->setRow(Array('id'=>'userid',...)); // create, returen primary key * $this->straight->setRow(Array('id'=>'userid',...), 1); // update, returen primary key * $this->straight->setRow(Array('id'=>'userid',...), Array('key'=>'val')); * $this->straight->unsetRow(1); // delete, return Boolean * $this->straight->unsetRow(Array('key'=>'val')); * $this->straight->getRow(1); // getRow, returen Array * $this->straight->getRow(Array('key'=>'val')); * $this->straight->getRows(30, 0); // getRows, limit=30, offset=0, return Array * $this->straight->getRows(30, 0, Array('key'=>'val')); * $this->straight->getRowsCnt(); // getRowsCnt, getRows like Where, return Numeric * $this->straight->getRowsCnt(Array('key'=>'val')); * $this->straight->getLists(30, 0, Array('key'=>>'val')); // getRows, getRowsCnt */ class Straight_model extends CI_Model { public $table = 'user'; public function __construct($table=''){ parent::__construct(); // default table user if( ! empty($table) ){ $this->setTable($table); } } /** * set TableName * * @param string $table * @throws Exception */ public function setTable($table=''){ if( empty($table) ){ Throw New Exception('Emtpy Table Name'); } $this->table = $table; } /** * asset where * @param Multitype:numeric|Array|Object $where * @throws Exception */ private function where($where=Array()){ switch( TRUE ){ // 배열, 객체 case( is_array($where) || is_object($where) ): $this->db->where($where); break; // 숫자, 문자 case( ! empty($where) ): $this->db->where('idx', $where); break; default: Throw New Exception('Unknown Primary Key'); break; } } /** * set Row * * @param Array $set * @param Multitype:numeric|Array|Object $where * @throws Exception * @return numeric */ public function setRow($set=Array(), $where=Array()){ if( empty($set) || sizeof($set) < 1 ){ Throw New Exception('Empty Set Value'); } // 생성 if( empty($where) ){ $this->db ->from( $this->table ) ->set($set) ->set('created', 'CURRENT_TIMESTAMP', FALSE ) ->set('created_ip', "INET_ATON('".$this->input->ip_address()."')", FALSE) ->insert(); if( $this->db->_error_number() ){ Throw New Exception( $this->db->_error_message(), $this->db->_error_number() ); } $idx = $this->db->insert_id(); // 업데이트 }else{ // PK 확인 if( is_numeric($where) ){ $idx = $where; $this->db->where('idx', $idx); }else{ $idx = $where['idx']; $this->where($where); } $this->db ->from( $this->table ) ->set($set) ->update(); if( $this->db->_error_number() ){ Throw New Exception( $this->db->_error_message(), $this->db->_error_number() ); } } return $idx; } /** * unset Row * * @param Multitype:numeric|Array|Object $where * @throws Exception * @return boolean */ public function unsetRow($where=Array()){ $this->where($where); $rs = $this->db ->from( $this->table ) ->delete(); if( $this->db->_error_number() ){ Throw New Exception( $this->db->_error_message(), $this->db->_error_number() ); } return $rs; } /** * get Row * @param Multitype:numeric|Array|Object $where * @throws Exception * @return Array */ public function getRow($where=Array()){ $this->where($where); $rs = $this->db ->from( $this->table ) ->get() ->row_array(); if( $this->db->_error_number() ){ Throw New Exception( $this->db->_error_message(), $this->db->_error_number() ); } return $rs; } /** * get Rows comnon Where * * @param Multitype:numeric|Array|Object $where * @return boolean */ private function getRowsWhere($where=Array()){ try{ $this->where($where); }catch(Exception $e){ // 예외처리시 검색조건 없음 처리 return FALSE; } } /** * get Rows * * @param number $limit * @param number $offset * @param Multitype:numeric|Array|Object $where * @return multitype:|multitype:Array */ public function getRows($limit=15, $offset=0, $where=Array()){ $rs = Array(); $this->getRowsWhere($where); $qs = $this->db ->from( $this->table ) ->order_by('zidx') ->offset( $offset ) ->limit( $limit ) ->get(); if( $this->db->_error_number() ){ Throw New Exception( $this->db->_error_message(), $this->db->_error_number() ); } if( $qs === NULL || ! $qs->num_rows ){ return $rs; } // return $qs foreach( $qs->result_array() AS $row ){ $rs[] = $row; } return $rs; } /** * get Rows Cnt * * @param Multitype:numeric|Array|Object $where * @return numeric */ public function getRowsCnt($where=Array()){ $rs = 0; $this->getRowsWhere($where); $rs = $this->db ->from( $this->table ) ->count_all_results(); if( $this->db->_error_number() ){ Throw New Exception( $this->db->_error_message(), $this->db->_error_number() ); } return $rs; } public function getLists($limit=15, $offset=0, $where=Array() ){ $rs = Array( 'total' => 0, 'lists' => Array(), ); $rs['total'] = $this->getRowsCnt( $where ); if( $rs['total'] < 1 ){ return $rs; } $rs['lists'] = $this->getRows( $limit, $offset, $where ); return $rs; } } /* End of file straight_model.php */ /* Locatin: models/straight_model.php */

from http://bluebreeze.co.kr/793 by ccl(A) rewrite - 2020-03-06 03:20:38

댓글

이 블로그의 인기 게시물

[PHP] 코드이그니터 - 파일업로드 구현

[PHP] 코드이그니터 - 파일업로드 구현 파일 업로드 이번에 PHP 프레임워크인 코드 이그니터(Codeigniter)를 사용하여 홈페이지를 만드는데 사용한 이미지 업로드용 코드 입니다. upload 라이브러리를 사용하고 app~ 와 같은 위치에 upload 폴더를 만드고 다음 코드를 사용한다음 ajax 로 호출하여 파일을 업로드 합니다. function index() { // Upload 설정 $config['upload_path'] = './upload/'; $config[\'allowed_types\'] = 'gif|jpg|png'; $config['max_size'] = 100; // 100k $config['max_width'] = 1024; $config['max_height'] = 768; $this->load->library('upload', $config); $data = array(); if (! $this->upload->do_upload("service_image")) { $error = array('error' => $this->upload->display_errors()); } else { //$data = array('upload_data' => $this->upload->data()); $this->output->set_output("./upload/".$this->upload->data('file_name')); } } jquery 를 이용한 파일 업로드 호출 코드 function upload() { var datas, xhr; datas = new FormData(); datas.append( 'service_image', $( ...

2017년 1월 스타트업에서 구인할때 주로 원하는 개발 기술

2017년 1월 스타트업에서 구인할때 주로 원하는 개발 기술 php mysql linux android git kotlin gcm/fcm python mssql mongodb amazon aws ios objective-c swift github python c++ django python postgresql amazon aws html5/css3/javascript android java mysql python c++ c# java aws cloud-server dbms node.js postgresql redis nginx react.js hapi.js amazon aws restful-api angularJS jQuery html5/css3/javascript android firebase custom ui component restful-api asp.net c# html css javascript bootstrap angularjs node.js php mongodb redis 프론트엔드 주요 기술 javascript jquery ajax angularjs wbesocket html5/css3/javascript android ios java xcode node.js coffeescript mysql amazon ec2 amazon es3 android ios node.js php python java ios php mysql apache python android redis node.js jquery msa node.js java restful-api linux nosql golang redis nginx ...

이클립스 코드이그나이터 연동 ( eclipse codeigniter )

이클립스 코드이그나이터 연동 ( eclipse codeigniter ) https://ellislab.com/codeigniter/user-guide/installation/downloads.html 위의 사이트에서 코드이그나이터를 다운 받는다. 다운받은 압축 파일을 풀어 준다. 이클립스에서 php 프로젝트를 생성한 공간에 코드이그나이터 압축파일을 복사 붙여넣기 해준다. 위와 같은 화면이 나오면 정상적으로 연동이 된 것 입니다. from http://nahosung.tistory.com/22 by ccl(A) rewrite - 2020-03-06 16:20:55