123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- /**
- * 简易数据库操作类
- *
- * @author Jinhui Zhu<jinhui.zhu@live.cn>2015-12-10 14:35
- *
- * usage:
- *
- * @method setPrivateKey(array $data);
- */
- namespace Qii\Driver;
- use Qii\Exceptions\InvalidParams;
- /**
- * 用法 :
- * 首先需要创建 _ormRules 文件 rules/user.php
- use Qii\Base\Rules;
- class user extends Rules {
- public $table = 'user';
- pubic function fields(){
- return ['uid', 'nickname', 'password'];
- }
- public function constants() {
- $this->addRequiredFields(
- ['id', '用户ID不能为空'],
- ['nickname', '昵称不能为空'],
- ['password', '密码不能为空'],
- );
- $this->addNumberFields(
- ['uid', '用户ID必须是数字']
- );
- $this->addSafeFields(
- ['nickname', '昵称格式不正确,不能包含怪字符']
- );
- $this->addPasswordFields(
- ['password', '密码格式不正确']
- );
- }
- }
- use Qii\Driver\EasyDrive;
- class user extends EasyDrive {
- public $_ormRules = 'rules\user';
- public function __construct()
- {
- parent::__construct();
- }
- }
- $user = new user();
- $user->uid = 1;
- $user->nickname = 'admin';
- $user->password = md5('asdfadsfasdadfadf');
- $response = $user->save();
- $user->setPrivatekey(['uid']);
- $response = $user->exist();
- if($response->isError()) {
- return ['code' => $response->getCode(), 'msg' => $response->getMessage()]
- }
- if($response->count() > 0) {
- return $response->getResult();
- }
- *
- *
- */
- class EasyDrive {
- public $_ormRules = '';
- protected $_easyORM;
- protected $db;
- /**
- * @throws InvalidParams
- */
- public function __construct(){
- if ($this->_ormRules == '') {
- throw new InvalidParams('_ormRules is null');
- }
- $this->_easyORM = new \Qii\Driver\EasyORM(_loadClass($this->_ormRules));
- $this->db = $this->getDB();
- }
- /**
- * 获取DB属性
- * @return mixed
- */
- public function getDB() {
- return $this->_easyORM->getDB();
- }
- /**
- * 获取规则
- *
- * @return mixed
- */
- public function getRules() {
- return $this->_easyORM->getRules();
- }
- /**
- * 获取所有的字段
- *
- * @return mixed
- */
- public function getFields() {
- return $this->_easyORM->getFields();
- }
- /**
- * 设置表名,将rules暴露到外边
- *
- * @param string $table 表名
- * @return mixed
- */
- public function setTable($table) {
- $this->_easyORM->setTable($table);
- return $this;
- }
- /**
- * 返回表名
- *
- * @return mixed
- */
- public function getTable() {
- return $this->_easyORM->getTable();
- }
- /**
- * 获取字段及值
- *
- * @return mixed
- */
- public function getValues() {
- return $this->_easyORM->getValues();
- }
- public function __set($name, $value) {
- $this->_easyORM->$name = $value;
- }
- /**
- * 设置主键
- *
- * @param array $array 数组形式
- * @return EasyORM
- */
- public function setPrivateKey($array) {
- $this->_easyORM->setPrivateKey($array);
- return $this;
- }
- /**
- * 设置排除的内容
- *
- * @param array $array
- * @return $this
- */
- public function setExclude($array) {
- $this->_easyORM->setExcluded($array);
- return $this;
- }
- /**
- * __call 自动调用_easyORM中的方法
- *
- * @param string $method
- * @param mixed $args
- * @return mixed
- */
- public function __call($method, $args) {
- return call_user_func_array(array($this->_easyORM, $method), $args);
- }
- }
|