Model.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. return $this;
  94. }
  95. /**
  96. * 获取当前使用的数据库
  97. */
  98. public function getUseDB()
  99. {
  100. return $this->db->useDB;
  101. }
  102. /**
  103. * 设置规则
  104. * @param array $rules
  105. */
  106. public function setRules(\Qii\Driver\Rules $rules)
  107. {
  108. if (empty($rules)) throw new \Exception(\Qii::i('Please set rules first'), __LINE__);
  109. $this->rules = $rules;
  110. return $this;
  111. }
  112. /**
  113. * 生成数据库结构
  114. */
  115. public function tableStruct()
  116. {
  117. $this->checkRulesInstance();
  118. $struct = array_flip($this->rules->getFields());
  119. foreach ($struct AS $key => $val) {
  120. $struct[$key] = '';
  121. }
  122. return $struct;
  123. }
  124. /**
  125. * 检查是否已经设置规则
  126. */
  127. final public function checkRulesInstance()
  128. {
  129. if ($this->rules == null) throw new \Exception(\Qii::i('Please set rules first'), __LINE__);
  130. }
  131. /**
  132. * 获取当前初始化的model
  133. * @return \Qii_Driver_Easy
  134. */
  135. final public function getInstance()
  136. {
  137. $this->checkRulesInstance();
  138. $tableName = $this->rules->getTableName();
  139. if(!isset($this->model[$tableName])) $this->model[$tableName] = _DBDriver($this->rules);
  140. return $this->model[$tableName];
  141. }
  142. /**
  143. * 设置主键
  144. * @param array $privateKey 设置主键
  145. * @return Object
  146. */
  147. final public function setPrivateKey($privateKey = array())
  148. {
  149. $this->getInstance()->setPrivateKey($privateKey);
  150. return $this;
  151. }
  152. /**
  153. * 检查数据是否存在
  154. * @param array $fields 数据
  155. * @return \Qii\Driver\Response
  156. */
  157. final public function _exist($fields, $privateKey = array())
  158. {
  159. return $this->getInstance()->setPrivateKey($privateKey)->setFieldsVal($fields)->_exist();
  160. }
  161. /**
  162. * 保存数据
  163. * @param array $fields 数据
  164. * @return \Qii\Driver\Response
  165. */
  166. final public function _save($fields, $privateKey = array())
  167. {
  168. return $this->getInstance()->setPrivateKey($privateKey)->setFieldsVal($fields)->_save();
  169. }
  170. /**
  171. * 更新数据
  172. * @param array $fields 数据
  173. * @return \Qii\Driver\Response
  174. */
  175. final public function _update($fields, $privateKey = array())
  176. {
  177. return $this->getInstance()->setPrivateKey($privateKey)->setFieldsVal($fields)->_update();
  178. }
  179. /**
  180. * 删除数据
  181. * @param array $fields 数据
  182. * @return \Qii_Response
  183. */
  184. final public function _remove($fields, $privateKey = array())
  185. {
  186. return $this->getInstance()->setPrivateKey($privateKey)->setFieldsVal($fields)->_remove();
  187. }
  188. /**
  189. * 方法不存在的时候调用$this->db下的方法
  190. * @param string $method 方法名
  191. * @param mix $args 参数
  192. */
  193. public function __call($method, $args)
  194. {
  195. if ($this->db) return call_user_func_array(array($this->db, $method), $args);
  196. }
  197. }