php+pdo+fetch+obj,PHP: PDOStatement::fetchObject - Manual

You can access MySQL tables in an objective way. Suppose you have a table named Users that has fields: UserID, UserName, UserPassword, UserBirthday, you can create a PHP class extending DataObject that is associated with this table:

<?phpclassUserextendsDataObject {// name: Table Name, key: Primary Key (can be an array), auto: AUTO_INCREMENT fieldprotected static$_table= array('name'=>'Users','key'=>'UserID','auto'=>'UserID');// relationships between PHP properties and MySQL field namesprotected static$_propertyList= array('id'=>'UserID','name'=>'UserName','password'=>'UserPassword','birthday'=>'UserBirthday');// A method that fetches all users as an arraypublic static functionGetAll() {

global$dbh;$sql='SELECT * FROM Users';$stmt=$dbh->query($sql);$users= array();

while ($user=$stmt->fetchObject(__CLASS__)) {$users[] =$user;

}

return$users;

}// Methods that fetch a specific userpublic static functionGetUserByName($name) {}

public static functionGetUserByID($name) {}// Methods for the current user objectpublic functioncheckPassword($password) {return$this->password==$password;}

public functionshowLink() {return"id}\">{$this->name}";}

}// Then, you can create an instance of this class to insert a row in your table$user= newUser();$user->name='oct1158';$user->password='789012';$user->useFunction('birthday','NOW()');

echo'Field birthday uses MySQL Function: ',$user->birthday,'
';

if ($user->insert()) {

echo'New User ID: ',$user->id,'
';// Update the row$user->password='112233';$user->update();

} else {

echo'INSERT Failed
';

}// Get a specific user by a query$sql='SELECT * FROM Users WHERE UserName = ?';$stmt=$dbh->prepare($sql);$stmt->execute(array('admin'));$admin_user=$stmt->fetchObject('User');

echo'Admin ID is ',$admin_user->id,'.
';

echo'Admin Birthday is ',$admin_user->birthday,'.
';// Get all users by a static method of that class$users=User::GetAll();

echo'
';

echo$users[0]->name,', ',$users[0]->birthday,'
';

echo$users[1]->name,', ',$users[1]->birthday,'
';

echo$users[2]->name,', ',$users[2]->birthday,'
';

echo'
';// Create an empty user and then delete it immediately$user= newUser();$user->insert();$user->delete();?>The DataObject class example:

private$changedFields= array();// list of updated fieldsprivate$data= array();// original row from PDOStatementprivate$funcFields= array();// fields that use MySQL functions

// The properties above are private in this class, so even if in your subclass you define some properties named the same, or you associate a property of the same name with a field in your table, they will never influence these properties.function__get($property) {

if (isset($this::$_propertyList[$property])) {

return$this->data[$this::$_propertyList[$property]];// access fields by PHP properties} else {

return$this->$property;// throw the default PHP error}

}

function__set($property,$value) {

if (isset($this::$_propertyList[$property])) {$field=$this::$_propertyList[$property];$this->data[$field] =$value;// update $data

// take down changed fieldsif (!in_array($field,$this->changedFields)) {array_push($this->changedFields,$field);

}$index=array_search($field,$this->funcFields);

if ($index!==false) {

unset($this->funcFields[$index]);$this->funcFields=array_values($this->funcFields);

}

} else {// For fetchObject$this->data[$property] =$value;// redirect to Array $data}

}

private functioncheckPrimaryKey() {}

private functionclear() {}

public functiondelete() {}

public functioninsert() {}

public functionupdate() {}

public functionuseFunction($property,$function) {}

}?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值