tablepre = TABLE_PREFIX;
$this->class = get_class($this);
$this->table = $this->tablepre . strtolower($this->class);
$this->data = array();
$this->connect();
}
private function connect() {
if(!self::$link) {
self::$link = mysql_connect(DBHOST, DBUSER, DBPASS);
mysql_select_db(DBNAME);
}
return self::$link;
}
public function __set($name, $value) {
$this->data[$name] = $value;
}
private function implodefields($cond) {
$fields = array();
foreach($cond as $key => $value) {
$value = mysql_real_escape_string($value);
$fields[] = "`$key`='$value'";
}
return implode(', ', $fields);
}
public function add() {
$fields = $this->implodefields($this->data);
$sql = "INSERT INTO `{$this->table}` SET $fields";
$this->query($sql);
}
public function findById($id) {
$sql = "SELECT * FROM `{$this->table}` WHERE `{$this->primaryKey}`='$id' LIMIT 1";
$data = $this->getOne($sql);
return $this->makeObjFromArray($data);
}
private function makeObjFromArray($data) {
$obj = new $this->class;
foreach($data as $key => $value) {
$obj->$key = $value;
}
return $obj;
}
private function query($sql) {
echo $sql . "\n";
return mysql_query($sql, self::$link);
}
private function getOne($sql) {
$data = $this->query($sql);
if($data) {
$item = mysql_fetch_assoc($data);
return $item;
}
return false;
}
}
class User extends ActiveRecord {
var $primaryKey = 'id';
}
$user = new User();
$user->name = 'Woody';
$user->email = 'xxx@hotmail.com';
$user->add();
$user = $user->findById(1);
print_r($user);
原文地址: http://www.phpig.net/showthread.php?tid=310
Pingback: Leg Workouts For women
Pingback: Jack3d
Pingback: Linkaufbau
Pingback: Lisbon hotels
Pingback: make money with iphone apps
Pingback: lunettes
Pingback: ray-ban
Pingback: divorce law in pennsylvania
Pingback: hydroponics stores in california
Pingback: carrara marble tiles sydney
Pingback: Jimmy Choo Solbriller
Pingback: Kangaroo Island Holiday Homes
Pingback: http://www.banner4.com
Pingback: http://www.24hbailbonds.com/CA/whittier.htm
Pingback: divorce attorneys in pittsburgh
Pingback: gafas carrera jocker
Pingback: 21st century books
Pingback: tin bullion
Pingback: Shane Hackett Looking Glass Funds
Pingback: free web hosting
Pingback: dresses
Pingback: sexe adult
Pingback: adult social network