MY_Model拡張
想定している使い方。
$array = $this->childmodel->get_all();
$array = $this->childmodel->get_row_where(array('key' => 'value'));
<?php /** * MY_Model * * @author localdisk * @property CI_DB_active_record $db */ class MY_Model extends CI_Model { protected $_table; /** * dbクラスを読み込み、テーブル名を自分のクラス名として指定する。 */ public function __construct() { parent::__construct(); $this->load->database(); $class = get_class($this); $tablename = strtolower(substr($class, 0, strrpos($class, '_'))); $this->_table = $tablename; } /** * 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 get_all() { return $this->db->get($this->_table)->result_array(); } /** * キーを配列で受け取る * @param array $array * @return array */ public function get_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_row_id($id) { return $this->db->where(array('id' => $id))->get($this->_table)->row_array(); } public function get_row_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(); } /** * ユーザーIDが存在しているかチェックする * @param integer $user_id * @return integer */ public function check_user_id($user_id) { return $this->db->where(array('user_id' => $user_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(); } /** * Insert Update用。 * 渡されたハッシュをセットする。 * 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'); } }