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); } }