• 追加された行はこの色です。
  • 削除された行はこの色です。
[[CodeIgniter]]

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

- CodeIgniter の Model を一工夫して使いやすくする方法
http://localdisk.hatenablog.com/entries/2011/05/02

** MY_Modelの自作 [#ec5be745]

 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を使用する前提 [#z158718f]

- モデル名は「テーブル名_model」にすること
User というテーブルのモデルを作成する場合のクラス名は User_model になります

- id というカラムが存在すること
プライマリーキーですね。

- created_at, updated_at というカラムが存在すること
作成日時, 更新日時です。
すべてのテーブルに timestamp で定義してます。
updated_at は ON UPDATE CURRENT_TIMESTAMP にしてます。

- このmodelの肝
CodeIgniterのActiveRecordは、マニュアルや解説本だと
insert/update 時にデータを配列で渡していますが
別にクラスでもいけるっていうことです。

** MY_Modelの使い方 [#uc00ba72]

 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();
    }

}
}}

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

+ Sample_modelクラスのプロパティに、DBテーブルのカラム名と同じ名前を設定する。
+ 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