|
@@ -0,0 +1,422 @@
|
|
|
+<?php
|
|
|
+namespace Qii\Driver;
|
|
|
+
|
|
|
+use Qii\Exceptions\InvalidParams;
|
|
|
+use stdClass;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 使用方法
|
|
|
+ *
|
|
|
+ *
|
|
|
+ */
|
|
|
+final class EasyORM {
|
|
|
+ protected $_easyORM = array();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @throws InvalidParams
|
|
|
+ */
|
|
|
+ public function __construct(\Qii\Base\Rules $rules) {
|
|
|
+ $this->clean();
|
|
|
+ $this->_easyORM->db = \Qii::getInstance("\Qii\Driver\Model")->db;
|
|
|
+ $this->setRules($rules);
|
|
|
+ $this->setFields($rules->fields());
|
|
|
+ $this->setResponse(new Response());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取db属性
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function getDB() {
|
|
|
+ return $this->_easyORM->db;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置验证规则
|
|
|
+ *
|
|
|
+ * @param \Qii\Base\Rules $rules
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function setRules(\Qii\Base\Rules $rules) {
|
|
|
+ $this->_easyORM->rules = $rules;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回 rules
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function getRules() {
|
|
|
+ return $this->_easyORM->rules;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置需要操作的表名称
|
|
|
+ *
|
|
|
+ * @param string $table 表名
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function setTable($table) {
|
|
|
+ return $this->_easyORM->rules->setTable($table);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取表名
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function getTable() {
|
|
|
+ return $this->_easyORM->rules->getTable();
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 设置表字段
|
|
|
+ *
|
|
|
+ * @param array $fields
|
|
|
+ * @return void
|
|
|
+ * @throws
|
|
|
+ */
|
|
|
+ public function setFields($fields = array()) {
|
|
|
+ if(!is_array($fields)) {
|
|
|
+ throw new InvalidParams("数据表字段不能为空");
|
|
|
+ }
|
|
|
+ $this->_easyORM->fields = new Fields($fields);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取所有字段
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function getFields() {
|
|
|
+ return $this->_easyORM->fields->getFields();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取字段的值
|
|
|
+ * @param string $field 字段名称
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function getFieldValue($field) {
|
|
|
+ return $this->_easyORM->fields->getField($field);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取所有字段和值
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function getValues() {
|
|
|
+ return $this->_easyORM->fields->getValues();
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * @param Response $response
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function setResponse(Response $response) {
|
|
|
+ $this->_easyORM->response = $response;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return EasyORM
|
|
|
+ */
|
|
|
+ protected function clean(){
|
|
|
+ $this->_easyORM = new stdClass();
|
|
|
+ $this->_easyORM->rules = null;
|
|
|
+ $this->_easyORM->db = null;
|
|
|
+ $this->_easyORM->fields = null;
|
|
|
+ $this->_easyORM->response = null;
|
|
|
+ $this->_easyORM->privateKeys = array();
|
|
|
+ $this->_easyORM->exclude = array();
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置表字段
|
|
|
+ *
|
|
|
+ * @param $name
|
|
|
+ * @param $val
|
|
|
+ * @return $this
|
|
|
+ */
|
|
|
+ public function __set($name, $val)
|
|
|
+ {
|
|
|
+ $this->_easyORM->fields->hasField($name) && $this->_easyORM->fields->$name = $val;
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量设置值
|
|
|
+ *
|
|
|
+ * @param $values
|
|
|
+ * @return $this|void
|
|
|
+ */
|
|
|
+ public function setValues($values) {
|
|
|
+ if(!is_array($values)) return $this;
|
|
|
+ foreach ($values as $key => $value) {
|
|
|
+ $this->_easyORM->fields->hasField($key) && $this->_easyORM->fields->$key = $value;
|
|
|
+ }
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 设置 private key,主键必须有设置值,否则不接收
|
|
|
+ * @param array|string $keys
|
|
|
+ * @return EasyORM
|
|
|
+ */
|
|
|
+ public function setPrivateKey($keys = array())
|
|
|
+ {
|
|
|
+ if(empty($keys)) {
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+ if(!is_array($keys)) {
|
|
|
+ $this->_easyORM->fields->hasField($keys) && $this->_easyORM->privateKeys[] = $keys;
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+ foreach ($keys as $key) {
|
|
|
+ $this->_easyORM->fields->hasField($key) && $this->_easyORM->privateKeys[] = $key;
|
|
|
+ }
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 移除主键
|
|
|
+ *
|
|
|
+ * @param array|string $keys
|
|
|
+ * @return $this
|
|
|
+ */
|
|
|
+ public function removePrivateKey($keys = array()) {
|
|
|
+ if(!is_array(key)) {
|
|
|
+ if(isset($this->_easyORM->privateKeys[$keys])) unset($this->_easyORM->privateKeys[$keys]);
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+ foreach ($keys as $key) {
|
|
|
+ if(isset($this->_easyORM->privateKeys[$key])) unset($this->_easyORM->privateKeys[$key]);
|
|
|
+ }
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 获取主键对应的值
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ final protected function getPrivateValue()
|
|
|
+ {
|
|
|
+ $data = array();
|
|
|
+ foreach ($this->_easyORM->privateKeys AS $key) {
|
|
|
+ $data[$key] = $this->_easyORM->fields->getField($key);
|
|
|
+ }
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置排除的数据列
|
|
|
+ *
|
|
|
+ * @param array $values 值
|
|
|
+ * @return EasyORM
|
|
|
+ */
|
|
|
+ public function setExcluded($values = array()){
|
|
|
+ if(!is_array($values)) {
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+ foreach ($values as $key => $value) {
|
|
|
+ $this->_easyORM->fields->hasField($key) && $this->_easyORM->exclude[$key] = $value;
|
|
|
+ }
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return Qii\Driver\Response|void
|
|
|
+ */
|
|
|
+ public function save() {
|
|
|
+ $this->_easyORM->rules->addForceValidKey($this->_easyORM->rules->getValidFieldsForSave());
|
|
|
+ $this->_easyORM->rules->addValues($this->_easyORM->fields->getValueAsArray());
|
|
|
+ $verify = $this->_easyORM->rules->verify();
|
|
|
+ if(!$verify['valid']) {
|
|
|
+ return Response::FailValidate('validate', array('_result' => \Qii::i($verify['code'], '验证失败')
|
|
|
+ , 'fields' => array('field' => $verify['data'], 'message' => $verify['errorInfo'])));
|
|
|
+ }
|
|
|
+ if (($this->_easyORM->privateKeys || $this->_easyORM->exclude) && $this->exist()->count() > 0) {
|
|
|
+ return Response::Exist('save', array('_result' => \Qii::i(1511, join(',', $this->getPrivateValue()))));
|
|
|
+ }
|
|
|
+ $map = $this->_easyORM->rules->getValues();
|
|
|
+ $result = $this->_easyORM->db->insertObject($this->_easyORM->rules->getTable(), $map);
|
|
|
+ if($this->_easyORM->db->isError()) {
|
|
|
+ return $this->_easyORM->db->getResponse();
|
|
|
+ }
|
|
|
+ return Response::Success('save', array('_result' => $result));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新数据
|
|
|
+ *
|
|
|
+ * @return Qii\Driver\Response
|
|
|
+ */
|
|
|
+ public function update() {
|
|
|
+ $this->_easyORM->rules->addForceValidKey($this->_easyORM->rules->getValidFieldsForUpdate());
|
|
|
+ $this->_easyORM->rules->addValues($this->_easyORM->fields->getValueAsArray());
|
|
|
+ $verify = $this->_easyORM->rules->verify();
|
|
|
+ if(!$verify['valid']) {
|
|
|
+ return Response::FailValidate('validate', array('_result' => \Qii::i($verify['code'], '验证失败')
|
|
|
+ , 'fields' => array('field' => $verify['data'], 'message' => $verify['errorInfo'])));
|
|
|
+ }
|
|
|
+ if (($this->_easyORM->privateKeys || $this->_easyORM->exclude) && $this->exist()->count() == 0) {
|
|
|
+ return Response::NotExist('update', array('_result' => \Qii::i(1512, join(',', $this->getPrivateValue()))));
|
|
|
+ }
|
|
|
+ $map = $this->_easyORM->rules->getValues();
|
|
|
+ $result = $this->_easyORM->db->updateObject($this->_easyORM->rules->getTable(), $map, $this->getPrivateValue());
|
|
|
+ if ($this->_easyORM->db->isError()) {
|
|
|
+ return $this->_easyORM->db->getResponse();
|
|
|
+ }
|
|
|
+ return Response::Success('_update', array('_result' => $result));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除指定数据
|
|
|
+ *
|
|
|
+ * @return Qii\Driver\Response
|
|
|
+ */
|
|
|
+ final public function remove() {
|
|
|
+ if(!$this->_easyORM->privateKeys && !$this->_easyORM->exclude) {
|
|
|
+ return Response::FAIL('privateKey', \Qii::i(1513));
|
|
|
+ }
|
|
|
+ $fieldsAndValues = [];
|
|
|
+ foreach ($this->_easyORM->privateKeys as $key) {
|
|
|
+ $fieldsAndValues[$key] = $this->_easyORM->fields->getField($key);
|
|
|
+ }
|
|
|
+ $result = $this->_easyORM->db
|
|
|
+ ->where($fieldsAndValues)
|
|
|
+ ->exclude($this->_easyORM->exclude)
|
|
|
+ ->remove($this->_easyORM->rules->getTable());
|
|
|
+ return Response::Success('remove', array('_result' => $result));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查数据是否已经存在,并返回一行,只能根据主键查询
|
|
|
+ *
|
|
|
+ * @return \Qii\Driver\Response
|
|
|
+ */
|
|
|
+ final public function exist() {
|
|
|
+ if(!$this->_easyORM->privateKeys && !$this->_easyORM->exclude) {
|
|
|
+ return Response::FAIL('privateKey', \Qii::i(1513));
|
|
|
+ }
|
|
|
+ $fieldsAndValues = [];
|
|
|
+ foreach ($this->_easyORM->privateKeys as $key) {
|
|
|
+ $fieldsAndValues[$key] = $this->_easyORM->fields->getField($key);
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->_easyORM->rules->addValues($fieldsAndValues);
|
|
|
+ $verify = $this->_easyORM->rules->verify();
|
|
|
+ if(!$verify['valid']) {
|
|
|
+ return Response::FailValidate('validate', array('_result' => \Qii::i($verify['code'], $verify['msg'])
|
|
|
+ , 'fields' => array('field' => $verify['data'], 'message' => $verify['errorInfo'])));
|
|
|
+ }
|
|
|
+
|
|
|
+ if($this->_easyORM->exclude) {
|
|
|
+ $verify = $this->_easyORM->rules->verifyFields($this->_easyORM->exclude);
|
|
|
+ if (!$verify['valid']) return Response::FailValidate('validate', array('_result' => \Qii::i($verify['code'], $verify['msg'])
|
|
|
+ , 'fields' => array('field' => $verify['data'], 'message' => $verify['errorInfo'])));
|
|
|
+ }
|
|
|
+ $result = $this->_easyORM->db->limit(1)
|
|
|
+ ->where($fieldsAndValues)
|
|
|
+ ->exclude($this->_easyORM->exclude)
|
|
|
+ ->selectRow($this->_easyORM->rules->getTable());
|
|
|
+ return Response::Success('exist', array('_result' => $result));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 使用此方法用于查询某一条数据的某一个字段
|
|
|
+ * @useage getXxxByXxx _getXxxByxxx
|
|
|
+ * _getRowById:返回所有字段; _getRowsById:通过id获取所有匹配的数据
|
|
|
+ * _getRowsByFields(['user_id', 'email']) 返回所有匹配的行数据,可以带参数,带参数优先使用参数的字段查询,否则使用设置的fields查询
|
|
|
+ * _getRowByFields(['user_id', 'email']) 返回匹配的一行数据,可以带参数,带参数优先使用参数的字段查询,否则使用设置的fields查询
|
|
|
+ * _getRowByUserId(1) 通过user_id查询,返回所有字段
|
|
|
+ * getNameById:通过id获取name的值; getEmailById:通过id获取email;
|
|
|
+ * getEmailAddressByUserId 通过user_id查询email_address
|
|
|
+ * getEmailByUserId(1) 通过user_id查询,返回email字段
|
|
|
+ * 备注:以_开头的才会去走getRow, getRows方法去取所有字段,目前仅支持All, Row, Rows这几个方法
|
|
|
+ * @param string $method [description]
|
|
|
+ * @param mixed $args 请求的参数
|
|
|
+ * @return Response
|
|
|
+ */
|
|
|
+ final public function __call($method, $args)
|
|
|
+ {
|
|
|
+ $selectType = 'normal';
|
|
|
+ if (substr($method, 0, 1) == '_') {
|
|
|
+ $selectType = 'system';
|
|
|
+ $method = substr($method, 1);
|
|
|
+ }
|
|
|
+ preg_match('/^(get)(.*)(By)(.*)/', $method, $matches);
|
|
|
+
|
|
|
+ if ($matches && count($matches) == 5 && $matches[1] == 'get') {
|
|
|
+ //大写字母匹配下划线,字段中统一用小写字母,在查询的时候使用驼峰结构
|
|
|
+ //如:getEmailAddressByUserId 通过user_id查询email_address
|
|
|
+ $field = strtolower(preg_replace('/(?<!^)([A-Z])/', '_$1', $matches[2]));
|
|
|
+
|
|
|
+ $method = 'selectRow';
|
|
|
+ if ($field == 'rows' && $selectType == 'system') $method = 'selectRows';
|
|
|
+ if ($field == 'row' && $selectType == 'system') $method = 'selectRow';
|
|
|
+ if (in_array($field, array('all', 'row', 'rows')) && $selectType == 'system') {
|
|
|
+ $field = $this->getFields();
|
|
|
+ }
|
|
|
+ $value = $this->_easyORM->db->setQuote(array_shift($args));
|
|
|
+ $name = strtolower(strtolower(preg_replace('/(?<!^)([A-Z])/', '_$1', $matches[4])));
|
|
|
+ $whereArray = array();
|
|
|
+ if ($selectType == 'system' && $name == 'fields') {
|
|
|
+ //优先使用参数,参数没有就使用设置的fields
|
|
|
+ // _getRowsByFields() 返回所有匹配的行数据,可以带参数,带参数优先使用参数的字段查询,否则使用设置的fields查询
|
|
|
+ // _getRowByFields() 返回匹配的一行数据,可以带参数,带参数优先使用参数的字段查询,否则使用设置的fields查询
|
|
|
+ if(!$value) {
|
|
|
+ $value = $this->_easyORM->fields->getValues();
|
|
|
+ }
|
|
|
+ foreach ($value AS $val) {
|
|
|
+ $whereArray[$val] = $this->_easyORM->fields->hasField($val) ? $this->_easyORM->fields->getField($val) : '';
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $whereArray[$name] = $value;
|
|
|
+ }
|
|
|
+ $where = array();
|
|
|
+ foreach ($whereArray AS $key => $val) {
|
|
|
+ if($this->_easyORM->fields->hasField($key)) $where[$key] = $val;
|
|
|
+ }
|
|
|
+
|
|
|
+ $result = $this->_easyORM->db->fields($field)->where($where)->$method($this->_easyORM->rules->getTable());
|
|
|
+ if ($this->_easyORM->db->isError()) {
|
|
|
+ return $this->_easyORM->db->getResponse();
|
|
|
+ }
|
|
|
+ return Response::Success($method, array('_result' => $result));
|
|
|
+ }
|
|
|
+
|
|
|
+ preg_match('/^(exec)(.*)/', $method, $matches);
|
|
|
+
|
|
|
+ if ($matches && count($matches) == 3) {
|
|
|
+ $alias = lcfirst($matches[2]);
|
|
|
+ if (method_exists($this->_easyORM->db, $alias)) {
|
|
|
+ $result = $this->_easyORM->db->{$matches[2]}($this->_easyORM->rules->getTable(), $this->getFields());
|
|
|
+ if ($this->_easyORM->db->isError()) {
|
|
|
+ return $this->_easyORM->db->getResponse();
|
|
|
+ }
|
|
|
+ return Response::Success($matches[2], array('_result' => $result));
|
|
|
+ }
|
|
|
+ if ($this->_easyORM->db->getAlias($method) && method_exists($this->_easyORM->db, $this->_easyORM->db->getAlias($method))) {
|
|
|
+ $this->_easyORM->db->whereArray($this->getFields());
|
|
|
+ $result = $this->_easyORM->db->{$this->_easyORM->db->getAlias($method)}($this->_easyORM->rules->getTable());
|
|
|
+ if ($this->_easyORM->db->isError()) {
|
|
|
+ return $this->_easyORM->db->getResponse();
|
|
|
+ }
|
|
|
+ return Response::Success($this->_easyORM->db->getAlias($method), array('_result' => $result));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //访问方法的别名
|
|
|
+ if ($this->_easyORM->db->getAlias($method)) {
|
|
|
+ if (method_exists($this->_easyORM->db, $this->_easyORM->db->getAlias($method))) {
|
|
|
+ $result = call_user_func_array(array($this->_easyORM->db, $this->_easyORM->db->getAlias($method)), $args);
|
|
|
+ if ($this->_easyORM->db->isError()) {
|
|
|
+ return $this->_easyORM->db->getResponse();
|
|
|
+ }
|
|
|
+ return Response::Success($this->_easyORM->db->getAlias($method), array('_result' => $result));
|
|
|
+ }
|
|
|
+ \Qii::setError(false, __LINE__, 1106, 'Model', 'Alias ' . $this->_easyORM->db->getAlias($method) . ' does not exist.', print_r($args, true));
|
|
|
+ return Response::UndefinedMethod('__call', $this->_easyORM->db->getAlias($method));
|
|
|
+ }
|
|
|
+ \Qii::setError(false, __LINE__, 1106, 'Model', $method, print_r($args, true));
|
|
|
+ return Response::UndefinedMethod('__call', $method);
|
|
|
+ }
|
|
|
+}
|