CodeIgniterのコアクラス
CodeIgniterのコアクラス拡張覚書。
これをベースにアプリケーションごとに拡張する。
My_Controllerクラスの拡張
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /** * MY_Controller * * @author localdisk * * @property CI_Loader $load * @property CI_Input $input * @property CI_Session $session * @property CI_Config $config * @property CI_Form_validation $form_validation * @property CI_URI $uri * * */ class MY_Controller extends CI_Controller { private $layout = '', $title = ''; public function __construct() { parent::__construct(); //DB初期化 $this->load->database(); //共通ヘルパー $this->load->helper('url'); $this->load->helper('cookie'); //共通ライブラリ $this->load->library('form_validation'); $this->load->library('form_date'); //共通モデル } function _remap($method, $params = array()){ if(method_exists($this, $method)){ call_user_func_array(array($this, $method), $params); } else { //ビューを探す $view_file = FCPATH.APPPATH.'views/' .strtolower(get_class($this)).'/'.$method.'_view.php'; if(file_exists($view_file)){ $output = $this->view(strtolower(get_class($this)).'/'.$method, NULL, true); $this->put_layout($output); } else { show_404(); } } } protected function put_layout($data, $layout = NULL, $title = NULL){ //レイアウト指定 if ($layout != null ) $this->layout = $layout; //タイトル指定 if ($title != null ) $this->title = $title; //指定されてなければデフォルト if ($this->layout == '') $this->layout = '_default'; //Titleがセットされているか if($this->title != '') $this->title .= " - "; $this->title .= ""; //データがStringならArrayに if (is_string($data)) $output['data'] = $data; //print_a($output); $output['title'] = $this->title; $output['contents_title'] = ""; $this->view('layout/'.$this->layout, $output); } protected function set_title_name($name){ $this->title = $name; } /** * Layout set * @param string $layout */ protected function set_layout_name($layout){ $this->layout = $layout; } /** * * Model Load Wrapper * * @param string $name * @return MY_Model * @link http://elliotscode.com/2011/03/28/codeigniter-my-controller */ protected function model($name, $su_name = false) { if( $su_name === false ){ $su_name = $name; } $name = $name . '_model'; if( ! isset($this->{$name})) { $this->load->model($name, $su_name); } return $this->{$su_name}; } /** * * View Load Wrapper * * @param string $name * @param array $data * @param boolean $return * @return type */ protected function view($name, $data = array(), $return = FALSE) { $name = $name . '_view'; return $this->load->view($name, $data, $return); } }
My_Model の拡張
<?php /** * MY_Model * * @author localdisk * @property CI_DB_active_record $db */ class MY_Model extends CI_Model { /** * table name * * @var string */ protected $_table; /** * constructor */ public function __construct() { parent::__construct(); $this->load->database(); $clazz = get_class($this); $this->_table = strtolower(substr($clazz, 0, strrpos($clazz, '_'))); } /** * insert * * @return integer */ public function insert() { $now = $this->now(); $this->db->set(array('created_at' => $now, 'updated_at' => $now)); $ret = $this->db->insert($this->_table, $this); if ($ret === FALSE) { return FALSE; } return $this->db->insert_id(); } /** * update * * @param integer|string $id */ public function update($id, $data = null) { if ($data === null) { $data = $this; } $this->db->set(array('updated_at' => $this->now())); $ret = $this->db->update($this->_table, $data, array('id' => $id)); if ($ret === FALSE) { return FALSE; } } /** * update_userid * * @param integer|string $id */ public function update_userid($id, $data = null) { if ($data === null) { $data = $this; } $this->db->set(array('updated_at' => $this->now())); $ret = $this->db->update($this->_table, $data, array('user_id' => $id)); if ($ret === FALSE) { return FALSE; } } /** * delete * * @param integer|strng $id */ public function delete($id) { $this->db->delete($this->_table, array('id' => $id)); } /** * 全てを配列で返す * @return array */ public function all() { return $this->db->get($this->_table)->result_array(); } /** * キーを配列で受け取る * @param array $array * @return array */ public function all_where($array) { foreach($array as $k => $v){ $this->db->where($k, $v); } return $this->db->get($this->_table)->result_array(); } /** * IDを元にデータを配列で返す * @param integer|string $id * @return array */ public function get_id($id) { return $this->db->where(array('id' => $id))->get($this->_table)->row_array(); } public function get_where($array) { foreach($array as $k => $v){ $this->db->where($k, $v); } return $this->db->get($this->_table)->row_array(); } /** * IDが存在しているかチェックする * @param integer $id * @return integer */ public function check_id($id) { return $this->db->where(array('id' => $id))->from($this->_table)->count_all_results(); } public function check_where($array) { foreach($array as $k => $v){ $this->db->where($k, $v); } return $this->db->from($this->_table)->count_all_results(); } /** * 渡されたハッシュをセットする。 * Keyとフィールドが合わなければセットしない。 * @param array_hash $array */ public function set_array_data($array) { foreach ($array as $k => $v){ if($this->db->field_exists($k, $this->_table)){ $this->$k = $v; } } } /** * now * * @return string */ public function now() { return date('Y-m-d H:i:s'); } }