Model.php 5.5 KB

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