看板 Mesak
作者 標題 [PHP] Codeigniter CRUD Model
時間 2013年09月12日 Thu. PM 11:32:09
http://blog.builtbyprime.com/php/a-guide-to-generic-code-igniter-models
http://henrihnr.wordpress.com/2009/04/26/simple-crud-application/
參考上列兩個網站,跟之前寫的程式
先設定 core/My_model.php
<?
class My_Model extends CI_Model
{
}
model 下都使用繼承class My_Model extends CI_Model
{
public $table;
public $keyName;
public $identifierName;
function __construct( $table = '' , $identifierName = '' , $keyName = '' )
{
parent::__construct();
$this->table = $table;
$this->keyName = $keyName;
$this->identifierName = $identifierName;
}
function get($id = 0 ,$keyName = '')
{
$result = FALSE;
if( !empty( $id ) )
{
$keyName = empty( $keyName ) ? $this->identifierName : $keyName;
$query = $this->db->get_where($this->table,array( $keyName => $id ) );
$result = $query->first_row();
}
return $result;
}
function get_where($where = array() )
{
$result = FALSE;
if( is_array($where) && count($where) )
{
$query = $this->db->get_where($this->table,$where );
$result = $query->result_object();
}
return $result;
}
function delete($id = 0 ,$keyName = '')
{
$result = FALSE;
if( !empty( $id ) )
{
$keyName = empty( $keyName ) ? $this->identifierName : $keyName;
$result = $this->delete_where( array( $keyName => $id ) );
}
return $result;
}
function delete_where( $where = array() )
{
$result = FALSE;
if( is_array($where) && count($where))
{
$this->db->delete($this->table, $where);
$result = $this->db->affected_rows();
}
return $result;
}
function update($id = 0 ,$data = array() )
{
$result = FALSE;
if( !empty($id) && is_array($data) && count($data) )
{
$keyName = empty( $keyName ) ? $this->identifierName : $keyName;
$result = $this->update_where( array( $keyName => $id ) , $data);
}
return $result;
//$this->db->affected_rows();
}
function update_where($where = array() ,$data = array() )
{
$result = FALSE;
if( count($where ) && count( $data ) )
{
$result = $this->update($this->table,$where,$data);
}
return $result;
}
function insert($data = null)
{
$result = FALSE;
if( is_array($data) && count($data) )
{
$this->db->insert( $this->table , $data );
if( $this->db->affected_rows() > 0 )
{
$result = $this->db->insert_id();
}
}
return $result;
}
function get_count($where)
{
if( is_array($where) && count($where) )
{
$this->db->where($where);
}
return $this->db->count_all_results($this->table);
}
function get_list()
{
$result = array();
$query = $this->db
->select("$this->keyName , $this->identifierName")
->order_by( $this->identifierName ,'ASC')
->get($this->table);
if ($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
$result[ $row[$this->identifierName] ] = $row[$this->keyName];
}
}
return $result;
}
}
class Post_Model extends My_Model
{
function __construct()
{
parent::__construct('posts','ID','post_title');
}
}
{
function __construct()
{
parent::__construct('posts','ID','post_title');
//parent::__construct(資料表,ID,標題);
}
}
--
Mesak Blog
http://mesak.oow.me
--
※ 作者: mesak 時間: 2013-09-12 23:32:09
※ 編輯: mesak 時間: 2014-02-04 17:56:43
※ 看板: Mesak 文章推薦值: 0 目前人氣: 0 累積人氣: 342
回列表(←)
分享