CodeIgniter

CRUDメソッドの継承

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

MY_Modelの自作

application/core/MY_Model.php
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
<?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を使用する前提

  • モデル名は「テーブル名_model」にすること
    User というテーブルのモデルを作成する場合のクラス名は User_model になります
  • id というカラムが存在すること
    プライマリーキーですね。
  • created_at, updated_at というカラムが存在すること
    作成日時, 更新日時です。
    すべてのテーブルに timestamp で定義してます。
    updated_at は ON UPDATE CURRENT_TIMESTAMP にしてます。
  • このmodelの肝
    CodeIgniterのActiveRecordは、マニュアルや解説本だと
    insert/update 時にデータを配列で渡していますが
    別にクラスでもいけるっていうことです。

MY_Modelの使い方

application/models/sample_model.php
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
<?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
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
<?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 (3459d)