Model.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * 数据库分发器
  4. * @author Jinhui Zhu <jinhui.zhu@live.cn>2016-01-19 18:31
  5. * 使用方法:
  6. * 1.
  7. * namespace Model;
  8. *
  9. * use \Qii\Model;
  10. * class comments extends Model
  11. * {
  12. * public function __construct()
  13. * {
  14. * parent::__construct();
  15. * $this->setRules(new \Qii\Driver\Rules(\Qii\Autoloader\Import::includes('configure/table/adcycle.comments.config.php')));
  16. * }
  17. * }
  18. * 2.
  19. * $test = _M(new \Qii\Driver\Rules(\Qii\Autoloader\Import::includes('configure/table/test.config.php')));
  20. * $fields = array('id' => 1, 'name' => 'test');
  21. * $test->save($fields);
  22. */
  23. namespace Qii\Driver;
  24. class Model
  25. {
  26. const VERSION = '1.2';
  27. /**
  28. * @var $_allow 允许使用的数据库驱动类新
  29. */
  30. protected $_allow = array('pdo', 'mysql', 'mysqli');
  31. /**
  32. * @var $db 数据库实例
  33. */
  34. public $db = null;
  35. /**
  36. * 数据库配置文件
  37. */
  38. protected $_dbInfo;
  39. /**
  40. * 数据库驱动
  41. */
  42. protected $_driver = 'pdo';
  43. /**
  44. * @var $_load 加载类
  45. */
  46. public $_load;
  47. /**
  48. * @var $_language 语言包
  49. */
  50. public $_language;
  51. /**
  52. * @var array $rules 数据表规则
  53. */
  54. private $rules = null;
  55. /**
  56. * @var \Qii_Driver_Easy $model
  57. */
  58. private $model = array();
  59. /**
  60. * @var Qii_Request_Abstract $_request 请求类
  61. */
  62. protected $_request;
  63. /**
  64. * @var $_helper helper类
  65. */
  66. protected $_helper;
  67. public function __construct()
  68. {
  69. $this->_load = \Qii\Autoloader\Psr4::getInstance()->loadClass('\Qii\Autoloader\Loader');
  70. $this->_language = \Qii\Autoloader\Psr4::getInstance()->loadClass('\Qii\Language\Loader');
  71. $this->_request = \Qii\Autoloader\Psr4::getInstance()->loadClass('Qii\Request\Http');
  72. $this->_helper = \Qii\Autoloader\Psr4::getInstance()->loadClass('Qii\Autoloader\Helper');
  73. $this->_dbInfo = \Qii\Config\Register::getAppConfigure(\Qii\Config\Register::get(\Qii\Config\Consts::APP_DB));
  74. if (isset($this->_dbInfo['driver'])) {
  75. $this->_driver = $this->_dbInfo['driver'];
  76. }
  77. if (!in_array($this->_driver, $this->_allow)) {
  78. $this->_driver = array_shift($this->_allow);
  79. }
  80. \Qii\Autoloader\Import::requires(array(
  81. Qii_DIR . DS . 'Qii' . DS . 'Driver' . DS . 'Base.php',
  82. Qii_DIR . DS . 'Qii' . DS . 'Driver' . DS . 'ConnBase.php',
  83. Qii_DIR . DS . 'Qii' . DS . 'Driver' . DS . 'ConnIntf.php',
  84. Qii_DIR . DS . 'Qii' . DS . 'Driver' . DS . ucWords($this->_driver) . DS . 'Connection.php',
  85. Qii_DIR . DS . 'Qii' . DS . 'Driver' . DS . ucWords($this->_driver) . DS . 'Driver.php',
  86. ));
  87. $this->db = \Qii\Autoloader\Psr4::getInstance()->loadClass(
  88. '\Qii\Driver\\' . ucWords($this->_driver) . '\Driver',
  89. \Qii\Autoloader\Psr4::getInstance()->loadClass(
  90. '\Qii\Driver\\' . ucWords($this->_driver) . '\Connection'
  91. )
  92. );
  93. $this->db->_debugSQL = isset($this->_dbInfo['debug']) ? $this->_dbInfo['debug'] : false;
  94. return $this;
  95. }
  96. /**
  97. * 设置属性
  98. *
  99. * @param string $name 属性名
  100. * @param mix $val 值
  101. */
  102. public function __set($name, $val)
  103. {
  104. $this->db->$name = $val;
  105. }
  106. /**
  107. * 将属性转到DB类中去
  108. */
  109. public function __get($attr)
  110. {
  111. if ($this->db) {
  112. return $this->db->$attr;
  113. }
  114. return null;
  115. }
  116. /**
  117. * 获取当前使用的数据库
  118. */
  119. public function getCurrentDB()
  120. {
  121. return $this->db->currentDB;
  122. }
  123. /**
  124. * 设置规则
  125. * @param array $rules
  126. */
  127. public function setRules(\Qii\Driver\Rules $rules)
  128. {
  129. if (empty($rules)) throw new \Exception(\Qii::i('Please set rules first'), __LINE__);
  130. $this->rules = $rules;
  131. return $this;
  132. }
  133. /**
  134. * 生成数据库结构
  135. */
  136. public function tableStruct()
  137. {
  138. $this->checkRulesInstance();
  139. $struct = array_flip($this->rules->getFields());
  140. foreach ($struct AS $key => $val) {
  141. $struct[$key] = '';
  142. }
  143. return $struct;
  144. }
  145. /**
  146. * 检查是否已经设置规则
  147. */
  148. final public function checkRulesInstance()
  149. {
  150. if ($this->rules == null) throw new \Exception(\Qii::i('Please set rules first'), __LINE__);
  151. }
  152. /**
  153. * 获取当前初始化的model
  154. * @return \Qii_Driver_Easy
  155. */
  156. final public function getInstance()
  157. {
  158. $this->checkRulesInstance();
  159. $tableName = $this->rules->getTableName();
  160. if (!isset($this->model[$tableName])) $this->model[$tableName] = _DBDriver($this->rules);
  161. return $this->model[$tableName];
  162. }
  163. /**
  164. * 设置主键
  165. * @param array $privateKey 设置主键
  166. * @return Object
  167. */
  168. final public function setPrivateKey($privateKey = array())
  169. {
  170. $this->getInstance()->setPrivateKey($privateKey);
  171. return $this;
  172. }
  173. /**
  174. * 检查数据是否存在
  175. * @param array $fields 数据
  176. * @return \Qii\Driver\Response
  177. */
  178. final public function _exist($fields, $privateKey = array())
  179. {
  180. return $this->getInstance()->setPrivateKey($privateKey)->setFieldsVal($fields)->_exist();
  181. }
  182. /**
  183. * 保存数据
  184. * @param array $fields 数据
  185. * @return \Qii\Driver\Response
  186. */
  187. final public function _save($fields, $privateKey = array())
  188. {
  189. return $this->getInstance()->setPrivateKey($privateKey)->setFieldsVal($fields)->_save();
  190. }
  191. /**
  192. * 更新数据
  193. * @param array $fields 数据
  194. * @return \Qii\Driver\Response
  195. */
  196. final public function _update($fields, $privateKey = array())
  197. {
  198. return $this->getInstance()->setPrivateKey($privateKey)->setFieldsVal($fields)->_update();
  199. }
  200. /**
  201. * 删除数据
  202. * @param array $fields 数据
  203. * @return \Qii_Response
  204. */
  205. final public function _remove($fields, $privateKey = array())
  206. {
  207. return $this->getInstance()->setPrivateKey($privateKey)->setFieldsVal($fields)->_remove();
  208. }
  209. /**
  210. * 方法不存在的时候调用$this->db下的方法
  211. * @param string $method 方法名
  212. * @param mix $args 参数
  213. */
  214. public function __call($method, $args)
  215. {
  216. if ($this->db) return call_user_func_array(array($this->db, $method), $args);
  217. }
  218. }