class crecord extends crecord_NullObject {
var $model;
var $table;
var $row;
var $id;
function crecord()
{
$this->model = NULL;
$this->table = "";
$this->_common_init();
}
function _common_init( $id = NULL )
{
$this->row = NULL;
$this->id = $id;
if( $id != NULL )
$this->load($id);
}
function init( &$model , $table, $id = NULL)
{
$this->model = &$model;
$this->table = $table;
$this->_common_table();
$this->load($id);
}
function equal($record)
{
foreach($record->row as $key => $value)
{
if( $this->getField($key) != $value )
return false;
}
return true;
}
function reuse($id = NULL)
{
$this->_common_init($id);
}
function loadlast( $key = null , $value = null)
{
$id = $this->model->select_max('id', $key, $value);
$this->load($id);
}
function _common_table()
{
$this->model->init($this->table);
}
function load($id = NULL)
{
return $this->loadwithfield("id", $id);
}
function loadwithfield($field, $value)
{
$this->_common_table();
$this->reuse();
if( $field == NULL || $value == NULL )
return;
$row = $this->model->select_field_row($field,$value);
return $this->_init_load_field($row);
}
function _init_load_field($row)
{
if( $row == NULL )
{
$this->id = NULL;
return;
}
$this->id = $row->id;
$this->setrow($row);
return $this->row;
}
function load2($field ='id',$value=NULL)
{
return $this->loadwithfield($field, $value);
}
function is_update()
{
return $this->id != NULL;
}
function save()
{
if( $this->row == NULL )
return;
$this->_common_table();
if( $this->id == NULL )
{
$this->id = $this->model->insert($this->row);
return $this->id;
}
else
{
$this->model->update($this->id, $this->row);
return $this->id;
}
}
function delete($id = null)
{
$this->_common_table();
if( $id != null )
$this->load($id);
if( $this->id == NULL )
return;
$this->model->delete($this->id);
return $this->id;
}
function getField($field)
{
if( $field == "id" )
{
return $this->id;
}
switch( gettype($this->row) )
{
case "array":
return $this->row[$field];
case "object":
{
if( isset($this->row->$field) )
return $this->row->$field;
}
default:
return;
}
if( is_object($this->row))
return $this->row->$field;
if( is_array($this->row))
return $this->row[$field];
}
function setField($field, $value)
{
$type = gettype($this->row);
switch( $type)
{
case "array":
return $this->row[$field] = $value;
case "object":
default:
return $this->row->$field = $value;
}
}
function getrow()
{
$row = $this->row;
if( $row != NULL )
$row->id = $this->id;
return $row;
}
function setrow($row)
{
$this->_tranArr2Obj ($row);
}
function _tranArr2Obj($array_row)
{
foreach($array_row as $key => $value )
{
if( $key == "id" )
continue;
$this->setField($key, $value);
}
}
function create_owner()
{
return new crecord;
}
function isNull()
{
return "FALSE";
}
}
2011年6月20日 星期一
crecord
mymodel_db
class mymodel_db extends model{
var $table;
function mymodel_db(){
parent::model();
$this->table = "";
}
function init($table)
{
$this->table = $table;
}
function select($id)
{
$row = $this->select_field_row("id", $id);
return $row;
}
function select_query($field, $value)
{
$this->db->flush_cache();
$this->db->where($field , $value);
$query = $this->db->get($this->table);
return $query;
}
function select_field_row($field, $value)
{
$query = $this->select_query($field, $value);
return $query->row();
}
function select2($field,$value)
{
return $this->select_field_row($field, $value);
}
function delete($id){
$this->db->flush_cache();
$this->db->where("id",$id);
$this->db->delete($this->table);
}
function insert($row)
{
$this->db->flush_cache();
$this->db->insert($this->table,$row);
$id = $this->db->insert_id();
return $id;
}
function update($id, $row)
{
$this->db->flush_cache();
$this->db->where("id",$id);
$this->db->update($this->table,$row);
}
function selectall()
{
$this->db->flush_cache();
$query = $this->db->get($this->table);
return $query;
}
function select_all_idTOarr()
{
$id_arr = array();
$this->db->flush_cache();
$query = $this->db->get($this->table);
foreach($query->result() as $row)
{
$id_arr[] = $row->id;
}
return $id_arr;
}
function like_query($field, $value)
{
$this->db->flush_cache();
$this->db->like($field , $value);
$query = $this->db->get($this->table);
return $query;
}
function list_fields()
{
$result = $this->db->list_fields($this->table);
return $result;
}
}
訂閱:
意見 (Atom)