note 118864 added to pdostatement.fetchobject
| From: | zlk1214 at gmail dot com | Date: | Thu, 18 Feb 2016 13:42:01 +0000 |
| Subject: | note 118864 added to pdostatement.fetchobject | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-204984@lists.php.net to get a copy of this message | ||
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:
<?php
class User extends DataObject {
// name: Table Name, key: Primary Key (can be an array), auto: AUTO_INCREMENT field
protected static $_table = array('name' => 'Users', 'key' =>
'UserID', 'auto' => 'UserID');
// relationships between PHP properties and MySQL field names
protected static $_propertyList = array('id' => 'UserID', 'name'
=> 'UserName', 'password' => 'UserPassword',
'birthday' => 'UserBirthday');
// A method that fetches all users as an array
public static function GetAll() {
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 user
public static function GetUserByName($name) {}
public static function GetUserByID($name) {}
// Methods for the current user object
public function checkPassword($password) {return $this->password == $password;}
public function showLink() {return "<a
href=\"user.php?i={$this->id}\">{$this->name}</a>";}
}
// Then, you can create an instance of this class to insert a row in your table
$user = new User();
$user->name = 'oct1158';
$user->password = '789012';
$user->useFunction('birthday', 'NOW()');
echo 'Field birthday uses MySQL Function: ', $user->birthday, '<br>';
if ($user->insert()) {
echo 'New User ID: ', $user->id, '<br>';
// Update the row
$user->password = '112233';
$user->update();
} else {
echo 'INSERT Failed<br>';
}
// 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, '.<br>';
echo 'Admin Birthday is ', $admin_user->birthday, '.<br>';
// Get all users by a static method of that class
$users = User::GetAll();
echo '<br>';
echo $users[0]->name, ', ', $users[0]->birthday, '<br>';
echo $users[1]->name, ', ', $users[1]->birthday, '<br>';
echo $users[2]->name, ', ', $users[2]->birthday, '<br>';
echo '<br>';
// Create an empty user and then delete it immediately
$user = new User();
$user->insert();
$user->delete();
?>
The DataObject class example:
<?php
class DataObject {
private $changedFields = array(); // list of updated fields
private $data = array(); // original row from PDOStatement
private $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 fields
if (!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 function checkPrimaryKey() {}
private function clear() {}
public function delete() {}
public function insert() {}
public function update() {}
public function useFunction($property, $function) {}
}
?>
----
Server IP: 72.52.91.14
Probable Submitter: 112.195.152.41
----
Manual Page -- http://php.net/manual/en/pdostatement.fetchobject.php
Edit -- https://master.php.net/note/edit/118864
Del: integrated -- https://master.php.net/note/delete/118864/integrated
Del: useless -- https://master.php.net/note/delete/118864/useless
Del: bad code -- https://master.php.net/note/delete/118864/bad+code
Del: spam -- https://master.php.net/note/delete/118864/spam
Del: non-english -- https://master.php.net/note/delete/118864/non-english
Del: in docs -- https://master.php.net/note/delete/118864/in+docs
Del: other reasons-- https://master.php.net/note/delete/118864
Reject -- https://master.php.net/note/reject/118864
Search -- https://master.php.net/manage/user-notes.php