protected $settings;
protected $dbh;
+ protected $record_data;
+ protected $f_dirty;
+
public $id;
function __construct( $table, $property = null, $value = null ) {
+ $this->f_dirty = false;
+ $this->record_data = false;
+
$this->settings = Settings::factory();
$this->table = $this->settings->tbl_prefix.$table;
"='".mysql_real_escape_string( $value )."'";
$res = $this->__query( $sqlstr );
- $arr = mysql_fetch_array( $res , MYSQL_ASSOC );
- if( $arr === FALSE ) throw new exception( "construct:".$table."に".$property."=".$value."はありません" );
+ $this->record_data = mysql_fetch_array( $res , MYSQL_ASSOC );
+ if( $this->record_data === false ) throw new exception( "construct:".$table."に".$property."=".$value."はありません" );
// 最初にヒットした行のidを使用する
- $this->id = $arr['id'];
+ $this->id = $this->record_data['id'];
}
return;
}
function __set( $property, $value ) {
- if( $property == "id" ) throw new exception( "set:idの変更は不可" );
+ if( $property === "id" ) throw new exception( "set:idの変更は不可" );
// id = 0なら空の新規レコード作成
if( $this->id == 0 ) {
$sqlstr = "INSERT INTO ".$this->table." VALUES ( )";
$res = $this->__query( $sqlstr );
$this->id = mysql_insert_id();
+
+ // $this->record_data読み出し
+ $sqlstr = "SELECT * FROM ".$this->table.
+ " WHERE id = '".$this->id."'";
+
+ $res = $this->__query( $sqlstr );
+ $this->record_data = mysql_fetch_array( $res , MYSQL_ASSOC );
+ }
+ if( $this->record_data === false ) throw new exception("set: DBの異常?" );
+
+ if( array_key_exists( $property, $this->record_data ) ) {
+ $this->record_data[$property] = mysql_real_escape_string($value);
+ $this->f_dirty = true;
+ }
+ else {
+ throw new exception("set:$property はありません" );
}
- $sqlstr = "UPDATE ".$this->table." SET ".
- mysql_real_escape_string($property)."='".
- mysql_real_escape_string($value)."' WHERE id='".$this->id."'";
- $res = $this->__query( $sqlstr );
- if( $res == FALSE ) throw new exception("set:セット失敗" );
}
function __get( $property ) {
if( $this->id == 0 ) throw new exception( "get:無効なid" );
- if( $property == "id" ) return $this->id;
-
- $sqlstr = "SELECT ".mysql_real_escape_string($property)." FROM ".$this->table." WHERE id='".$this->id."'";
- $res = $this->__query($sqlstr);
- $arr = mysql_fetch_row( $res );
- if( $arr === FALSE ) throw new exception( "get:".$property."は存在しない" );
+ if( $property === "id" ) return $this->id;
+ if( $this->record_data === false ) throw new exception( "get: 無効なレコード" );
+ if( ! array_key_exists( $property, $this->record_data ) ) throw new exception( "get: $propertyは存在しません" );
- return stripslashes($arr[0]);
+ return stripslashes($this->record_data[$property]);
}
function delete() {
$sqlstr = "DELETE FROM ".$this->table." WHERE id='".$this->id."'";
$this->__query( $sqlstr );
$this->id = 0;
+ $this->record_data = false;
+ $this->f_dirty = false;
+ }
+
+ function close() {
+ if( $this->id != 0 ) {
+ if( $this->f_dirty ) {
+ $sqlstr = "UPDATE ".$this->table." SET";
+ foreach( $this->record_data as $property => $value ) {
+ if( $property === "id" ) continue;
+ $sqlstr .= " ".$property." = '".$value."',";
+ }
+ $sqlstr = rtrim($sqlstr, "," );
+ $sqlstr .= " WHERE id = '".$this->id."'";
+ $res = $this->__query($sqlstr);
+ if( $res === false ) throw new exception( "close: アップデート失敗" );
+ }
+ $this->id = 0;
+ $this->f_dirty = false;
+ $this->record_data = false;
+ }
}
// countを実行する
}
function __destruct() {
- $this->id = 0;
+ // 呼び忘れに対応
+ if( $this->id != 0 ) {
+ $this->close();
+ }
}
}
?>