CodeIgniter

CRUDメソッドの継承

CodeIgniterのmodel基底クラスを自作して、基本機能(CRUD)のメソッドを継承して利用する方法が紹介されていました。
継承すれば、重複するコードは書かなくて済むので、コーディングが楽になりますね?

MY_Modelの自作

application/core/MY_Model.php

#code(php){{

?php

/**

* MY_Model
*
* @author localdisk <info@localdisk.org>
* @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, strpos($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;
       }
       $ret = $this->db->update($this->_table, $data, array('id' => $id));
       if ($ret === FALSE) {
           return FALSE;
       }
   }
   /**
    * delete
    * 
    * @param integer|strng $id 
    */
   public function delete($id) {
       $this->db->delete($this->_table, array('id' => $id));
   }
   /**
    * find_all
    * 
    * @return array
    */
   public function find_all() {
       return $this->db->get($this->_table)->result();
   }
   /**
    * find_list
    * 
    * @param  integer|string $limit
    * @return array
    */
   public function find_list($limit = 10) {
       return $this->db->limit($limit)->order_by('id')->get($this->_table)->result();
   }
   /**
    * find
    * 
    * @param  integer|string $id
    * @return stdClass
    */
   public function find($id) {
       $ret = $this->db->where(array('id' => $id))->get($this->_table)->row();
       return $ret;
   }
   /**
    * now
    * 
    * @return string
    */
   public function now() {
       return date('Y-m-d H:i:s');
   }

}
}}

MY_Modelを使用する前提

MY_Modelの使い方

application/models/sample_model.php

#code(php){{

?php
/**

* Sample
*
* @author localdisk <info@localdisk.org>
*/

class Sample_model extends MY_Model {

   // カラムを public フィールドとして定義
   public $name;
   public $address;
   public function __construct() {
       parent::__construct();
   }

}
}}

はは~ん。なるほど!そういうことだったんですね!

  1. Sample_modelクラスのプロパティに、DBテーブルのカラム名と同じ名前を設定する。
  2. insert()に、$this(クラス自身を示す擬似変数)を渡せば、クラスのプロパティーが引数として渡せる。

なかなかスマートな方法だと思います。

application/controllers/sample.php

#code(php){{

?php
/**

* Sample
*
* @author localdisk <info@localdisk.org>
* @property Sample_model $sample
*/

class Sample extends MY_Controller {

   public function __construct() {
       parent::__construct();
   }
   public function sample() {
       // validation などは割愛
       $this->load->model('sample_model', 'sample');
       $this->sample->name = $this->input->post('name');
       $this->sample->address = $this->input->post('address');
       $this->sample->insert();
   }

}
}}


トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2014-11-03 (月) 22:31:04