PHP : ActiveRecord实现示例

时间:2023-03-10 07:20:35
PHP : ActiveRecord实现示例

先简单介绍一下Active Record:

Active Record(中文名:活动记录)是一种领域模型模式,特点是一个模型类对应关系型数据库中的一个表,而模型类的一个实例对应表中的一行记录。Active Record 和 Row Gateway (行记录入口)十分相似,但前者是领域模型,后者是一种数据源模式。关系型数据库往往通过外键来表述实体关系,Active Record 在数据源层面上也将这种关系映射为对象的关联和聚集。
Active Record 适合非常简单的领域需求,尤其在领域模型和数据库模型十分相似的情况下。如果遇到更加复杂的领域模型结构(例如用到继承、策略的领域模型),往往需要使用分离数据源的领域模型,结合 Data Mapper (数据映射器)使用。
Active Record 驱动框架一般兼有 ORM 框架的功能,但 Active Record 不是简单的 ORM,正如和 Row Gateway 的区别。著名的例子是全栈(Full Stack) Web 开发框架 Ruby on Rails ,其默认使用一个纯 Ruby 写成的 Active Record 框架来驱动 MVC 中的模型层。
在 Martin Fowler 的 《企业应用架构模式》 一书中曾详细叙述了本模式。
以下是著名的 Active Record 驱动框架:
SQLObject(Python)
Ruby on Rails ActiveRecord (Ruby)
Yii Framework ActiveRecord (PHP)
Castle ActiveRecord (.NET)
  1. <?php
  2. define('DBHOST', 'localhost');
  3. define('DBUSER', 'root');
  4. define('DBPASS', '');
  5. define('DBNAME', 'test');
  6. define('TABLE_PREFIX', 't_');
  7. class ActiveRecord {
  8. private $tablepre;
  9. private $class;
  10. private $table;
  11. private static $link;
  12. private $data;
  13. public $primaryKey = 'id';
  14. public function __construct() {
  15. $this->tablepre = TABLE_PREFIX;
  16. $this->class = get_class($this);
  17. $this->table = $this->tablepre . strtolower($this->class);
  18. $this->data = array();
  19. $this->connect();
  20. }
  21. private function connect() {
  22. if(!self::$link) {
  23. self::$link = mysql_connect(DBHOST, DBUSER, DBPASS);
  24. mysql_select_db(DBNAME);
  25. }
  26. return self::$link;
  27. }
  28. public function __set($name, $value) {
  29. $this->data[$name] = $value;
  30. }
  31. private function implodefields($cond) {
  32. $fields = array();
  33. foreach($cond as $key => $value) {
  34. $value = mysql_real_escape_string($value);
  35. $fields[] = "`$key`='$value'";
  36. }
  37. return implode(', ', $fields);
  38. }
  39. public function add() {
  40. $fields = $this->implodefields($this->data);
  41. $sql = "INSERT INTO `{$this->table}` SET $fields";
  42. $this->query($sql);
  43. }
  44. public function findById($id) {
  45. $sql = "SELECT * FROM `{$this->table}` WHERE `{$this->primaryKey}`='$id' LIMIT 1";
  46. $data = $this->getOne($sql);
  47. return $this->makeObjFromArray($data);
  48. }
  49. private function makeObjFromArray($data) {
  50. $obj = new $this->class;
  51. foreach($data as $key => $value) {
  52. $obj->$key = $value;
  53. }
  54. return $obj;
  55. }
  56. private function query($sql) {
  57. echo $sql . "\n";
  58. return mysql_query($sql, self::$link);
  59. }
  60. private function getOne($sql) {
  61. $data = $this->query($sql);
  62. if($data) {
  63. $item = mysql_fetch_assoc($data);
  64. return $item;
  65. }
  66. return false;
  67. }
  68. }
  69. class User extends ActiveRecord {
  70. var $primaryKey = 'id';
  71. }
  72. $user = new User();
  73. $user->name = '热电影';
  74. $user->email = 'www.redianying.com';
  75. $user->add();
  76. $user = $user->findById(1);
  77. print_r($user);