EasyDrive.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * 简易数据库操作类
  4. *
  5. * @author Jinhui Zhu<jinhui.zhu@live.cn>2015-12-10 14:35
  6. *
  7. * usage:
  8. *
  9. * @method setPrivateKey(array $data);
  10. */
  11. namespace Qii\Driver;
  12. use Qii\Exceptions\InvalidParams;
  13. /**
  14. * 用法 :
  15. * 首先需要创建 _ormRules 文件 rules/user.php
  16. use Qii\Base\Rules;
  17. class user extends Rules {
  18. public $table = 'user';
  19. pubic function fields(){
  20. return ['uid', 'nickname', 'password'];
  21. }
  22. public function constants() {
  23. $this->addRequiredFields(
  24. ['id', '用户ID不能为空'],
  25. ['nickname', '昵称不能为空'],
  26. ['password', '密码不能为空'],
  27. );
  28. $this->addNumberFields(
  29. ['uid', '用户ID必须是数字']
  30. );
  31. $this->addSafeFields(
  32. ['nickname', '昵称格式不正确,不能包含怪字符']
  33. );
  34. $this->addPasswordFields(
  35. ['password', '密码格式不正确']
  36. );
  37. }
  38. }
  39. use Qii\Driver\EasyDrive;
  40. class user extends EasyDrive {
  41. public $_ormRules = 'rules\user';
  42. public function __construct()
  43. {
  44. parent::__construct();
  45. }
  46. }
  47. $user = new user();
  48. $user->uid = 1;
  49. $user->nickname = 'admin';
  50. $user->password = md5('asdfadsfasdadfadf');
  51. $response = $user->save();
  52. $user->setPrivatekey(['uid']);
  53. $response = $user->exist();
  54. if($response->isError()) {
  55. return ['code' => $response->getCode(), 'msg' => $response->getMessage()]
  56. }
  57. if($response->count() > 0) {
  58. return $response->getResult();
  59. }
  60. *
  61. *
  62. */
  63. class EasyDrive {
  64. public $_ormRules = '';
  65. protected $_easyORM;
  66. protected $db;
  67. /**
  68. * @throws InvalidParams
  69. */
  70. public function __construct(){
  71. if ($this->_ormRules == '') {
  72. throw new InvalidParams('_ormRules is null');
  73. }
  74. $this->_easyORM = new \Qii\Driver\EasyORM(_loadClass($this->_ormRules));
  75. $this->db = $this->getDB();
  76. }
  77. /**
  78. * 获取DB属性
  79. * @return mixed
  80. */
  81. public function getDB() {
  82. return $this->_easyORM->getDB();
  83. }
  84. /**
  85. * 获取规则
  86. *
  87. * @return mixed
  88. */
  89. public function getRules() {
  90. return $this->_easyORM->getRules();
  91. }
  92. /**
  93. * 获取所有的字段
  94. *
  95. * @return mixed
  96. */
  97. public function getFields() {
  98. return $this->_easyORM->getFields();
  99. }
  100. /**
  101. * 设置表名,将rules暴露到外边
  102. *
  103. * @param string $table 表名
  104. * @return mixed
  105. */
  106. public function setTable($table) {
  107. $this->_easyORM->setTable($table);
  108. return $this;
  109. }
  110. /**
  111. * 返回表名
  112. *
  113. * @return mixed
  114. */
  115. public function getTable() {
  116. return $this->_easyORM->getTable();
  117. }
  118. /**
  119. * 获取字段及值
  120. *
  121. * @return mixed
  122. */
  123. public function getValues() {
  124. return $this->_easyORM->getValues();
  125. }
  126. public function __set($name, $value) {
  127. $this->_easyORM->$name = $value;
  128. }
  129. /**
  130. * 设置主键
  131. *
  132. * @param array $array 数组形式
  133. * @return EasyORM
  134. */
  135. public function setPrivateKey($array) {
  136. $this->_easyORM->setPrivateKey($array);
  137. return $this;
  138. }
  139. /**
  140. * 设置排除的内容
  141. *
  142. * @param array $array
  143. * @return $this
  144. */
  145. public function setExclude($array) {
  146. $this->_easyORM->setExcluded($array);
  147. return $this;
  148. }
  149. /**
  150. * __call 自动调用_easyORM中的方法
  151. *
  152. * @param string $method
  153. * @param mixed $args
  154. * @return mixed
  155. */
  156. public function __call($method, $args) {
  157. return call_user_func_array(array($this->_easyORM, $method), $args);
  158. }
  159. }