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
訂閱:
張貼留言 (Atom)
init 時 需要導入 mymodel_db
回覆刪除