Model.php 6.6 KB

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