Model.php 6.6 KB

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