首页 后端开发 php教程 Quentin Zervaas's DBO

Quentin Zervaas's DBO

Jul 25, 2016 am 09:10 AM

《Web 2.0 开发实战》一书的作者,Quentin Zervaas 在书中提到的一个简易PHP数据访问对象。
  1. /**
  2. * DatabaseObject
  3. *
  4. * Abstract class used to easily manipulate data in a database table
  5. * via simple load/save/delete methods
  6. */
  7. abstract class DatabaseObject
  8. {
  9. const TYPE_TIMESTAMP = 1;
  10. const TYPE_BOOLEAN = 2;
  11. protected static $types = array(self::TYPE_TIMESTAMP, self::TYPE_BOOLEAN);
  12. private $_id = null;
  13. private $_properties = array();
  14. protected $_db = null;
  15. protected $_table = '';
  16. protected $_idField = '';
  17. public function __construct(Zend_Db_Adapter_Abstract $db, $table, $idField)
  18. {
  19. $this->_db = $db;
  20. $this->_table = $table;
  21. $this->_idField = $idField;
  22. }
  23. public function load($id, $field = null)
  24. {
  25. if (strlen($field) == 0)
  26. $field = $this->_idField;
  27. if ($field == $this->_idField) {
  28. $id = (int) $id;
  29. if ($id return false;
  30. }
  31. $query = sprintf('select %s from %s where %s = ?',
  32. join(', ', $this->getSelectFields()),
  33. $this->_table,
  34. $field);
  35. $query = $this->_db->quoteInto($query, $id);
  36. return $this->_load($query);
  37. }
  38. protected function getSelectFields($prefix = '')
  39. {
  40. $fields = array($prefix . $this->_idField);
  41. foreach ($this->_properties as $k => $v)
  42. $fields[] = $prefix . $k;
  43. return $fields;
  44. }
  45. protected function _load($query)
  46. {
  47. $result = $this->_db->query($query);
  48. $row = $result->fetch();
  49. if (!$row)
  50. return false;
  51. $this->_init($row);
  52. $this->postLoad();
  53. return true;
  54. }
  55. public function _init($row)
  56. {
  57. foreach ($this->_properties as $k => $v) {
  58. $val = $row[$k];
  59. switch ($v['type']) {
  60. case self::TYPE_TIMESTAMP:
  61. if (!is_null($val))
  62. $val = strtotime($val);
  63. break;
  64. case self::TYPE_BOOLEAN:
  65. $val = (bool) $val;
  66. break;
  67. }
  68. $this->_properties[$k]['value'] = $val;
  69. }
  70. $this->_id = (int) $row[$this->_idField];
  71. }
  72. public function save($useTransactions = true)
  73. {
  74. $update = $this->isSaved();
  75. if ($useTransactions)
  76. $this->_db->beginTransaction();
  77. if ($update)
  78. $commit = $this->preUpdate();
  79. else
  80. $commit = $this->preInsert();
  81. if (!$commit) {
  82. if ($useTransactions)
  83. $this->_db->rollback();
  84. return false;
  85. }
  86. $row = array();
  87. foreach ($this->_properties as $k => $v) {
  88. if ($update && !$v['updated'])
  89. continue;
  90. switch ($v['type']) {
  91. case self::TYPE_TIMESTAMP:
  92. if (!is_null($v['value'])) {
  93. if ($this->_db instanceof Zend_Db_Adapter_Pdo_Pgsql)
  94. $v['value'] = date('Y-m-d H:i:sO', $v['value']);
  95. else
  96. $v['value'] = date('Y-m-d H:i:s', $v['value']);
  97. }
  98. break;
  99. case self::TYPE_BOOLEAN:
  100. $v['value'] = (int) ((bool) $v['value']);
  101. break;
  102. }
  103. $row[$k] = $v['value'];
  104. }
  105. if (count($row) > 0) {
  106. // perform insert/update
  107. if ($update) {
  108. $this->_db->update($this->_table, $row, sprintf('%s = %d', $this->_idField, $this->getId()));
  109. }
  110. else {
  111. $this->_db->insert($this->_table, $row);
  112. $this->_id = $this->_db->lastInsertId($this->_table, $this->_idField);
  113. }
  114. }
  115. // update internal id
  116. if ($commit) {
  117. if ($update)
  118. $commit = $this->postUpdate();
  119. else
  120. $commit = $this->postInsert();
  121. }
  122. if ($useTransactions) {
  123. if ($commit)
  124. $this->_db->commit();
  125. else
  126. $this->_db->rollback();
  127. }
  128. return $commit;
  129. }
  130. public function delete($useTransactions = true)
  131. {
  132. if (!$this->isSaved())
  133. return false;
  134. if ($useTransactions)
  135. $this->_db->beginTransaction();
  136. $commit = $this->preDelete();
  137. if ($commit) {
  138. $this->_db->delete($this->_table, sprintf('%s = %d', $this->_idField, $this->getId()));
  139. }
  140. else {
  141. if ($useTransactions)
  142. $this->_db->rollback();
  143. return false;
  144. }
  145. $commit = $this->postDelete();
  146. $this->_id = null;
  147. if ($useTransactions) {
  148. if ($commit)
  149. $this->_db->commit();
  150. else
  151. $this->_db->rollback();
  152. }
  153. return $commit;
  154. }
  155. public function isSaved()
  156. {
  157. return $this->getId() > 0;
  158. }
  159. public function getId()
  160. {
  161. return (int) $this->_id;
  162. }
  163. public function getDb()
  164. {
  165. return $this->_db;
  166. }
  167. public function __set($name, $value)
  168. {
  169. if (array_key_exists($name, $this->_properties)) {
  170. $this->_properties[$name]['value'] = $value;
  171. $this->_properties[$name]['updated'] = true;
  172. return true;
  173. }
  174. return false;
  175. }
  176. public function __get($name)
  177. {
  178. return array_key_exists($name, $this->_properties) ? $this->_properties[$name]['value'] : null;
  179. }
  180. protected function add($field, $default = null, $type = null)
  181. {
  182. $this->_properties[$field] = array('value' => $default,
  183. 'type' => in_array($type, self::$types) ? $type : null,
  184. 'updated' => false);
  185. }
  186. protected function preInsert()
  187. {
  188. return true;
  189. }
  190. protected function postInsert()
  191. {
  192. return true;
  193. }
  194. protected function preUpdate()
  195. {
  196. return true;
  197. }
  198. protected function postUpdate()
  199. {
  200. return true;
  201. }
  202. protected function preDelete()
  203. {
  204. return true;
  205. }
  206. protected function postDelete()
  207. {
  208. return true;
  209. }
  210. protected function postLoad()
  211. {
  212. return true;
  213. }
  214. public static function BuildMultiple($db, $class, $data)
  215. {
  216. $ret = array();
  217. if (!class_exists($class))
  218. throw new Exception('Undefined class specified: ' . $class);
  219. $testObj = new $class($db);
  220. if (!$testObj instanceof DatabaseObject)
  221. throw new Exception('Class does not extend from DatabaseObject');
  222. foreach ($data as $row) {
  223. $obj = new $class($db);
  224. $obj->_init($row);
  225. $ret[$obj->getId()] = $obj;
  226. }
  227. return $ret;
  228. }
  229. }
复制代码
  1. class DatabaseObject_User extends DatabaseObject
  2. {
  3. static $userTypes = array('member' => 'Member',
  4. 'administrator' => 'Administrator');
  5. public $profile = null;
  6. public $_newPassword = null;
  7. public function __construct($db)
  8. {
  9. parent::__construct($db, 'users', 'user_id');
  10. $this->add('username');
  11. $this->add('password');
  12. $this->add('user_type', 'member');
  13. $this->add('ts_created', time(), self::TYPE_TIMESTAMP);
  14. $this->add('ts_last_login', null, self::TYPE_TIMESTAMP);
  15. $this->profile = new Profile_User($db);
  16. }
  17. protected function preInsert()
  18. {
  19. $this->_newPassword = Text_Password::create(8);
  20. $this->password = $this->_newPassword;
  21. return true;
  22. }
  23. protected function postLoad()
  24. {
  25. $this->profile->setUserId($this->getId());
  26. $this->profile->load();
  27. }
  28. protected function postInsert()
  29. {
  30. $this->profile->setUserId($this->getId());
  31. $this->profile->save(false);
  32. $this->sendEmail('user-register.tpl');
  33. return true;
  34. }
  35. protected function postUpdate()
  36. {
  37. $this->profile->save(false);
  38. return true;
  39. }
  40. protected function preDelete()
  41. {
  42. $this->profile->delete();
  43. return true;
  44. }
  45. public function sendEmail($tpl)
  46. {
  47. $templater = new Templater();
  48. $templater->user = $this;
  49. // fetch the e-mail body
  50. $body = $templater->render('email/' . $tpl);
  51. // extract the subject from the first line
  52. list($subject, $body) = preg_split('/\r|\n/', $body, 2);
  53. // now set up and send the e-mail
  54. $mail = new Zend_Mail();
  55. // set the to address and the user's full name in the 'to' line
  56. $mail->addTo($this->profile->email,
  57. trim($this->profile->first_name . ' ' .
  58. $this->profile->last_name));
  59. // get the admin 'from' details from the config
  60. $mail->setFrom(Zend_Registry::get('config')->email->from->email,
  61. Zend_Registry::get('config')->email->from->name);
  62. // set the subject and body and send the mail
  63. $mail->setSubject(trim($subject));
  64. $mail->setBodyText(trim($body));
  65. $mail->send();
  66. }
  67. public function createAuthIdentity()
  68. {
  69. $identity = new stdClass;
  70. $identity->user_id = $this->getId();
  71. $identity->username = $this->username;
  72. $identity->user_type = $this->user_type;
  73. $identity->first_name = $this->profile->first_name;
  74. $identity->last_name = $this->profile->last_name;
  75. $identity->email = $this->profile->email;
  76. return $identity;
  77. }
  78. public function loginSuccess()
  79. {
  80. $this->ts_last_login = time();
  81. unset($this->profile->new_password);
  82. unset($this->profile->new_password_ts);
  83. unset($this->profile->new_password_key);
  84. $this->save();
  85. $message = sprintf('Successful login attempt from %s user %s',
  86. $_SERVER['REMOTE_ADDR'],
  87. $this->username);
  88. $logger = Zend_Registry::get('logger');
  89. $logger->notice($message);
  90. }
  91. static public function LoginFailure($username, $code = '')
  92. {
  93. switch ($code) {
  94. case Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND:
  95. $reason = 'Unknown username';
  96. break;
  97. case Zend_Auth_Result::FAILURE_IDENTITY_AMBIGUOUS:
  98. $reason = 'Multiple users found with this username';
  99. break;
  100. case Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID:
  101. $reason = 'Invalid password';
  102. break;
  103. default:
  104. $reason = '';
  105. }
  106. $message = sprintf('Failed login attempt from %s user %s',
  107. $_SERVER['REMOTE_ADDR'],
  108. $username);
  109. if (strlen($reason) > 0)
  110. $message .= sprintf(' (%s)', $reason);
  111. $logger = Zend_Registry::get('logger');
  112. $logger->warn($message);
  113. }
  114. public function fetchPassword()
  115. {
  116. if (!$this->isSaved())
  117. return false;
  118. // generate new password properties
  119. $this->_newPassword = Text_Password::create(8);
  120. $this->profile->new_password = md5($this->_newPassword);
  121. $this->profile->new_password_ts = time();
  122. $this->profile->new_password_key = md5(uniqid() .
  123. $this->getId() .
  124. $this->_newPassword);
  125. // save new password to profile and send e-mail
  126. $this->profile->save();
  127. $this->sendEmail('user-fetch-password.tpl');
  128. return true;
  129. }
  130. public function confirmNewPassword($key)
  131. {
  132. // check that valid password reset data is set
  133. if (!isset($this->profile->new_password)
  134. || !isset($this->profile->new_password_ts)
  135. || !isset($this->profile->new_password_key)) {
  136. return false;
  137. }
  138. // check if the password is being confirm within a day
  139. if (time() - $this->profile->new_password_ts > 86400)
  140. return false;
  141. // check that the key is correct
  142. if ($this->profile->new_password_key != $key)
  143. return false;
  144. // everything is valid, now update the account to use the new password
  145. // bypass the local setter as new_password is already an md5
  146. parent::__set('password', $this->profile->new_password);
  147. unset($this->profile->new_password);
  148. unset($this->profile->new_password_ts);
  149. unset($this->profile->new_password_key);
  150. // finally, save the updated user record and the updated profile
  151. return $this->save();
  152. }
  153. public function usernameExists($username)
  154. {
  155. $query = sprintf('select count(*) as num from %s where username = ?',
  156. $this->_table);
  157. $result = $this->_db->fetchOne($query, $username);
  158. return $result > 0;
  159. }
  160. static public function IsValidUsername($username)
  161. {
  162. $validator = new Zend_Validate_Alnum();
  163. return $validator->isValid($username);
  164. }
  165. public function __set($name, $value)
  166. {
  167. switch ($name) {
  168. case 'password':
  169. $value = md5($value);
  170. break;
  171. case 'user_type':
  172. if (!array_key_exists($value, self::$userTypes))
  173. $value = 'member';
  174. break;
  175. }
  176. return parent::__set($name, $value);
  177. }
  178. }
  179. ?>
复制代码


本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

在PHP API中说明JSON Web令牌(JWT)及其用例。 在PHP API中说明JSON Web令牌(JWT)及其用例。 Apr 05, 2025 am 12:04 AM

JWT是一种基于JSON的开放标准,用于在各方之间安全地传输信息,主要用于身份验证和信息交换。1.JWT由Header、Payload和Signature三部分组成。2.JWT的工作原理包括生成JWT、验证JWT和解析Payload三个步骤。3.在PHP中使用JWT进行身份验证时,可以生成和验证JWT,并在高级用法中包含用户角色和权限信息。4.常见错误包括签名验证失败、令牌过期和Payload过大,调试技巧包括使用调试工具和日志记录。5.性能优化和最佳实践包括使用合适的签名算法、合理设置有效期、

会话如何劫持工作,如何在PHP中减轻它? 会话如何劫持工作,如何在PHP中减轻它? Apr 06, 2025 am 12:02 AM

会话劫持可以通过以下步骤实现:1.获取会话ID,2.使用会话ID,3.保持会话活跃。在PHP中防范会话劫持的方法包括:1.使用session_regenerate_id()函数重新生成会话ID,2.通过数据库存储会话数据,3.确保所有会话数据通过HTTPS传输。

描述扎实的原则及其如何应用于PHP的开发。 描述扎实的原则及其如何应用于PHP的开发。 Apr 03, 2025 am 12:04 AM

SOLID原则在PHP开发中的应用包括:1.单一职责原则(SRP):每个类只负责一个功能。2.开闭原则(OCP):通过扩展而非修改实现变化。3.里氏替换原则(LSP):子类可替换基类而不影响程序正确性。4.接口隔离原则(ISP):使用细粒度接口避免依赖不使用的方法。5.依赖倒置原则(DIP):高低层次模块都依赖于抽象,通过依赖注入实现。

在PHPStorm中如何进行CLI模式的调试? 在PHPStorm中如何进行CLI模式的调试? Apr 01, 2025 pm 02:57 PM

在PHPStorm中如何进行CLI模式的调试?在使用PHPStorm进行开发时,有时我们需要在命令行界面(CLI)模式下调试PHP�...

如何在系统重启后自动设置unixsocket的权限? 如何在系统重启后自动设置unixsocket的权限? Mar 31, 2025 pm 11:54 PM

如何在系统重启后自动设置unixsocket的权限每次系统重启后,我们都需要执行以下命令来修改unixsocket的权限:sudo...

解释PHP中的晚期静态绑定(静态::)。 解释PHP中的晚期静态绑定(静态::)。 Apr 03, 2025 am 12:04 AM

静态绑定(static::)在PHP中实现晚期静态绑定(LSB),允许在静态上下文中引用调用类而非定义类。1)解析过程在运行时进行,2)在继承关系中向上查找调用类,3)可能带来性能开销。

如何用PHP的cURL库发送包含JSON数据的POST请求? 如何用PHP的cURL库发送包含JSON数据的POST请求? Apr 01, 2025 pm 03:12 PM

使用PHP的cURL库发送JSON数据在PHP开发中,经常需要与外部API进行交互,其中一种常见的方式是使用cURL库发送POST�...

See all articles