|
@@ -16,10 +16,10 @@ final class EasyORM {
|
|
|
* @throws InvalidParams
|
|
|
*/
|
|
|
public function __construct(\Qii\Base\Rules $rules) {
|
|
|
+ $this->_easyORM = new stdClass();
|
|
|
$this->clean();
|
|
|
+ $this->cleanCondition();
|
|
|
$this->_easyORM->db = \Qii::getInstance("\Qii\Driver\Model")->db;
|
|
|
- $this->_easyORM->queryFields = "*";
|
|
|
- $this->_easyORM->where = [];
|
|
|
$this->setRules($rules);
|
|
|
$this->setFields($rules->fields());
|
|
|
$this->setResponse(new Response());
|
|
@@ -30,7 +30,7 @@ final class EasyORM {
|
|
|
*
|
|
|
* @return mixed
|
|
|
*/
|
|
|
- public function getDB() {
|
|
|
+ final public function getDB() {
|
|
|
return $this->_easyORM->db;
|
|
|
}
|
|
|
|
|
@@ -40,8 +40,9 @@ final class EasyORM {
|
|
|
* @param \Qii\Base\Rules $rules
|
|
|
* @return void
|
|
|
*/
|
|
|
- public function setRules(\Qii\Base\Rules $rules) {
|
|
|
+ final public function setRules(\Qii\Base\Rules $rules) {
|
|
|
$this->_easyORM->rules = $rules;
|
|
|
+ return $this;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -49,7 +50,7 @@ final class EasyORM {
|
|
|
*
|
|
|
* @return mixed
|
|
|
*/
|
|
|
- public function getRules() {
|
|
|
+ final public function getRules() {
|
|
|
return $this->_easyORM->rules;
|
|
|
}
|
|
|
|
|
@@ -59,8 +60,9 @@ final class EasyORM {
|
|
|
* @param string $table 表名
|
|
|
* @return mixed
|
|
|
*/
|
|
|
- public function setTable($table) {
|
|
|
- return $this->_easyORM->rules->setTable($table);
|
|
|
+ final public function setTable($table) {
|
|
|
+ $this->_easyORM->rules->setTable($table);
|
|
|
+ return $this;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -68,7 +70,7 @@ final class EasyORM {
|
|
|
*
|
|
|
* @return mixed
|
|
|
*/
|
|
|
- public function getTable() {
|
|
|
+ final public function getTable() {
|
|
|
return $this->_easyORM->rules->getTable();
|
|
|
}
|
|
|
/**
|
|
@@ -78,19 +80,20 @@ final class EasyORM {
|
|
|
* @return void
|
|
|
* @throws
|
|
|
*/
|
|
|
- public function setFields($fields = array()) {
|
|
|
+ final public function setFields($fields = array()) {
|
|
|
if(!is_array($fields)) {
|
|
|
throw new InvalidParams("数据表字段不能为空");
|
|
|
}
|
|
|
$this->_easyORM->fields = new Fields($fields);
|
|
|
+ return $this;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置查询的 Fields
|
|
|
- * @param $fields
|
|
|
+ * @param array|string $fields
|
|
|
* @return $this
|
|
|
*/
|
|
|
- public function setQueryFields($fields) {
|
|
|
+ final public function setQueryFields($fields) {
|
|
|
if(!$fields) return $this;
|
|
|
if(is_array($fields)) {
|
|
|
$this->_easyORM->queryFields = join(",", $fields);
|
|
@@ -100,15 +103,30 @@ final class EasyORM {
|
|
|
return $this;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 清除 fields
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ final public function cleanQueryFields() {
|
|
|
+ $this->_easyORM->queryFields = '*';
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* @param array $args [[key, $value],...]
|
|
|
* @return EasyORM
|
|
|
*/
|
|
|
- public function where($args) {
|
|
|
+ final public function where($args) {
|
|
|
if(!is_array($args)) {
|
|
|
return $this;
|
|
|
}
|
|
|
- $this->_easyORM->where = $args;
|
|
|
+ $this->_easyORM->where = array();
|
|
|
+ foreach ($args as $key => $val) {
|
|
|
+ $explode = explode(":", $key)[0];
|
|
|
+ if($this->_easyORM->fields->hasField($explode)) {
|
|
|
+ $this->_easyORM->where[$key] = $val;
|
|
|
+ }
|
|
|
+ }
|
|
|
return $this;
|
|
|
}
|
|
|
|
|
@@ -116,10 +134,20 @@ final class EasyORM {
|
|
|
* 清除where条件
|
|
|
* @return $this
|
|
|
*/
|
|
|
- public function cleanWhere() {
|
|
|
+ final public function cleanWhere() {
|
|
|
$this->_easyORM->where = array();
|
|
|
return $this;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 清除like
|
|
|
+ *
|
|
|
+ * @return $this
|
|
|
+ */
|
|
|
+ final public function cleanLike() {
|
|
|
+ $this->_easyORM->like = array();
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
/**
|
|
|
* 获取所有字段
|
|
|
*
|
|
@@ -173,7 +201,6 @@ final class EasyORM {
|
|
|
* @return void
|
|
|
*/
|
|
|
public function __unset($name) {
|
|
|
- echo 'unset';
|
|
|
if(isset($this->_easyORM->fields->$name)) unset($this->_easyORM->fields->$name);
|
|
|
}
|
|
|
/**
|
|
@@ -199,29 +226,56 @@ final class EasyORM {
|
|
|
*
|
|
|
* @return mixed
|
|
|
*/
|
|
|
- public function getValueAsArray() {
|
|
|
+ final public function getValueAsArray() {
|
|
|
return $this->_easyORM->fields->getValueAsArray();
|
|
|
}
|
|
|
/**
|
|
|
* @param Response $response
|
|
|
* @return void
|
|
|
*/
|
|
|
- public function setResponse(Response $response) {
|
|
|
+ final public function setResponse(Response $response) {
|
|
|
$this->_easyORM->response = $response;
|
|
|
+ return $response;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @return EasyORM
|
|
|
*/
|
|
|
- protected function clean(){
|
|
|
- $this->_easyORM = new stdClass();
|
|
|
- $this->_easyORM->rules = null;
|
|
|
- $this->_easyORM->db = null;
|
|
|
- $this->_easyORM->fields = null;
|
|
|
+ final public function clean(){
|
|
|
$this->_easyORM->limit = array();
|
|
|
$this->_easyORM->response = null;
|
|
|
$this->_easyORM->privateKeys = array();
|
|
|
+ $this->_easyORM->where = array();
|
|
|
+ $this->_easyORM->like = array();
|
|
|
$this->_easyORM->exclude = array();
|
|
|
+ $this->_easyORM->orderBy = array();
|
|
|
+ $this->_easyORM->queryFields = '*';
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 清空所有的条件和字段的值
|
|
|
+ *
|
|
|
+ * @return EasyORM
|
|
|
+ */
|
|
|
+ final public function cleanCondition() {
|
|
|
+ $this->cleanPrivateKey();
|
|
|
+ $this->cleanWhere();
|
|
|
+ $this->cleanLike();
|
|
|
+ $this->cleanLimit();
|
|
|
+ $this->cleanExcluded();
|
|
|
+ $this->cleanQueryFields();
|
|
|
+ $this->cleanOrderBy();
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 清除所有设置了字段的值
|
|
|
+ *
|
|
|
+ * @return EasyORM
|
|
|
+ */
|
|
|
+ final public function cleanFieldsVal(){
|
|
|
+ $this->_easyORM->fields->cleanAllFieldsVal();
|
|
|
return $this;
|
|
|
}
|
|
|
|
|
@@ -229,9 +283,9 @@ final class EasyORM {
|
|
|
* 批量设置值
|
|
|
*
|
|
|
* @param $values
|
|
|
- * @return $this|void
|
|
|
+ * @return EasyORM
|
|
|
*/
|
|
|
- public function setValues($values) {
|
|
|
+ final 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;
|
|
@@ -243,7 +297,7 @@ final class EasyORM {
|
|
|
* @param array|string $keys
|
|
|
* @return EasyORM
|
|
|
*/
|
|
|
- public function setPrivateKey($keys = array())
|
|
|
+ final public function setPrivateKey($keys = array())
|
|
|
{
|
|
|
if(empty($keys)) {
|
|
|
return $this;
|
|
@@ -263,7 +317,7 @@ final class EasyORM {
|
|
|
*
|
|
|
* @return mixed
|
|
|
*/
|
|
|
- public function getPrivateKey() {
|
|
|
+ final public function getPrivateKey() {
|
|
|
return $this->_easyORM->privateKeys;
|
|
|
}
|
|
|
|
|
@@ -271,9 +325,9 @@ final class EasyORM {
|
|
|
* 移除主键
|
|
|
*
|
|
|
* @param array|string $keys
|
|
|
- * @return $this
|
|
|
+ * @return EasyORM
|
|
|
*/
|
|
|
- public function removePrivateKey($keys = array()) {
|
|
|
+ final public function removePrivateKey($keys = array()) {
|
|
|
if(!is_array(key)) {
|
|
|
if(isset($this->_easyORM->privateKeys[$keys])) unset($this->_easyORM->privateKeys[$keys]);
|
|
|
return $this;
|
|
@@ -298,9 +352,9 @@ final class EasyORM {
|
|
|
/**
|
|
|
* 清除主键
|
|
|
*
|
|
|
- * @return $this
|
|
|
+ * @return EasyORM
|
|
|
*/
|
|
|
- public function cleanPrivateKey() {
|
|
|
+ final public function cleanPrivateKey() {
|
|
|
$this->_easyORM->privateKeys = array();
|
|
|
return $this;
|
|
|
}
|
|
@@ -311,7 +365,7 @@ final class EasyORM {
|
|
|
* @param array $values 值
|
|
|
* @return EasyORM
|
|
|
*/
|
|
|
- public function setExcluded($values = array()){
|
|
|
+ final public function setExcluded($values = array()){
|
|
|
if(!is_array($values)) {
|
|
|
return $this;
|
|
|
}
|
|
@@ -324,17 +378,38 @@ final class EasyORM {
|
|
|
/**
|
|
|
* 清除排除选项
|
|
|
*
|
|
|
- * @return void
|
|
|
+ * @return EasyORM
|
|
|
*/
|
|
|
- public function cleanExcluded() {
|
|
|
+ final public function cleanExcluded() {
|
|
|
$this->_easyORM->exclude = array();
|
|
|
return $this;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @return Qii\Driver\Response|void
|
|
|
+ * 设置limit限制
|
|
|
+ *
|
|
|
+ * @param int $start limit start
|
|
|
+ * @param int $size size
|
|
|
+ * @return EasyORM
|
|
|
*/
|
|
|
- public function save() {
|
|
|
+ final public function setLimit($start, $size = 20) {
|
|
|
+ $this->_easyORM->limit = [$start, $size];
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 清除limit限制
|
|
|
+ * @return EasyORM
|
|
|
+ */
|
|
|
+ final public function cleanLimit() {
|
|
|
+ $this->_easyORM->limit = array();
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return Qii\Driver\Response|mixed
|
|
|
+ */
|
|
|
+ final public function save() {
|
|
|
$this->_easyORM->rules->addForceValidKey($this->_easyORM->rules->getValidFieldsForSave());
|
|
|
$this->_easyORM->rules->addValues($this->_easyORM->fields->getValueAsArray());
|
|
|
$verify = $this->_easyORM->rules->verify();
|
|
@@ -342,7 +417,7 @@ final class EasyORM {
|
|
|
return Response::FailValidate('validate', array('_result' => \Qii::i($verify['code'], $verify['msg'])
|
|
|
, 'fields' => array('field' => $verify['data'], 'message' => $verify['errorInfo']), 'message' => $verify['msg']));
|
|
|
}
|
|
|
- if (($this->_easyORM->privateKeys || $this->_easyORM->exclude) && $this->exist()->count() > 0) {
|
|
|
+ if (($this->_easyORM->privateKeys || $this->_easyORM->exclude || $this->_easyORM->where) && $this->exist()->count() > 0) {
|
|
|
return Response::Exist('save', array('_result' => \Qii::i(1511, join(',', $this->getPrivateValue()))));
|
|
|
}
|
|
|
$map = $this->_easyORM->rules->getValues();
|
|
@@ -354,13 +429,14 @@ final class EasyORM {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * like(['key'])->count() like(['key']->lists()
|
|
|
+ * like([['key' => 'value'], ...]->count() like([['key' => 'value'], ...]->lists()
|
|
|
* @param $values
|
|
|
- * @return Qii\Driver\Response
|
|
|
+ * @return EasyORM
|
|
|
*/
|
|
|
- public function like($values = array()) {
|
|
|
+ final public function like($values = array()) {
|
|
|
+ //未指定like字段
|
|
|
if(!is_array($values)) {
|
|
|
- return Response::Fail('like', array('_result' => '未指定like字段'));
|
|
|
+ return $this;
|
|
|
}
|
|
|
$query = array();
|
|
|
foreach ($values as $key => $value) {
|
|
@@ -369,7 +445,7 @@ final class EasyORM {
|
|
|
}
|
|
|
$query[$key] = $value;
|
|
|
}
|
|
|
- $this->_easyORM->db->like($query);
|
|
|
+ $this->_easyORM->like = $query;
|
|
|
return $this;
|
|
|
}
|
|
|
|
|
@@ -378,17 +454,26 @@ final class EasyORM {
|
|
|
*
|
|
|
* @return Qii\Driver\Response
|
|
|
*/
|
|
|
- public function count() {
|
|
|
+ final public function count() {
|
|
|
$query = $this->getValueAsArray();
|
|
|
if($this->_easyORM->where && is_array($this->_easyORM->where)) {
|
|
|
foreach ($this->_easyORM->where as $key => $value) {
|
|
|
$query[$key] = $value;
|
|
|
}
|
|
|
}
|
|
|
+ //字段值作为where条件, 仅list和count用
|
|
|
+ $value = $this->getValueAsArray();
|
|
|
+ foreach ($value as $key => $val) {
|
|
|
+ $query[$key] = $val;
|
|
|
+ }
|
|
|
$result = $this->_easyORM->db->fields(' count(1) as count')->limit(1)
|
|
|
->where($query)
|
|
|
+ ->like($this->_easyORM->like)
|
|
|
->exclude($this->_easyORM->exclude)
|
|
|
->selectOne($this->_easyORM->rules->getTable());
|
|
|
+ if ($this->_easyORM->db->isError()) {
|
|
|
+ return $this->_easyORM->db->getResponse();
|
|
|
+ }
|
|
|
return Response::Success('exist', array('_result' => $result));
|
|
|
}
|
|
|
/**
|
|
@@ -396,7 +481,7 @@ final class EasyORM {
|
|
|
*
|
|
|
* @return Qii\Driver\Response
|
|
|
*/
|
|
|
- public function update() {
|
|
|
+ final public function update() {
|
|
|
$this->_easyORM->rules->addForceValidKey($this->_easyORM->rules->getValidFieldsForUpdate());
|
|
|
$this->_easyORM->rules->addValues($this->_easyORM->fields->getValueAsArray());
|
|
|
$verify = $this->_easyORM->rules->verify();
|
|
@@ -407,8 +492,13 @@ final class EasyORM {
|
|
|
if (($this->_easyORM->privateKeys || $this->_easyORM->exclude) && $this->exist()->count() == 0) {
|
|
|
return Response::NotExist('update', array('_result' => \Qii::i(1512, join(',', $this->getPrivateValue()))));
|
|
|
}
|
|
|
+ $where = $this->getWhereCondition();
|
|
|
+ //merge like 字段
|
|
|
+ if($this->_easyORM->like) {
|
|
|
+ $where = array_merge($this->_easyORM->like, $where);
|
|
|
+ }
|
|
|
$map = $this->_easyORM->rules->getValues();
|
|
|
- $result = $this->_easyORM->db->updateObject($this->_easyORM->rules->getTable(), $map, $this->getPrivateValue());
|
|
|
+ $result = $this->_easyORM->db->updateObject($this->_easyORM->rules->getTable(), $map, $where);
|
|
|
if ($this->_easyORM->db->isError()) {
|
|
|
return $this->_easyORM->db->getResponse();
|
|
|
}
|
|
@@ -416,43 +506,128 @@ final class EasyORM {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 设置limit限制
|
|
|
+ * 指定字段的值 + step
|
|
|
*
|
|
|
- * @param int $start limit start
|
|
|
- * @param int $size size
|
|
|
- * @return $this
|
|
|
+ * $attr = new EasyDrive();
|
|
|
+ * $attr->visit = 1;
|
|
|
+ * $attr->where(['uid' => 1])->incr();
|
|
|
+ *
|
|
|
+ * @return mixed|Qii\Driver\Response
|
|
|
*/
|
|
|
- public function setLimit($start, $size = 20) {
|
|
|
- $this->_easyORM->limit = [$start, $size];
|
|
|
- return $this;
|
|
|
+ final public function incr() {
|
|
|
+ $value = $this->_easyORM->fields->getValueAsArray();
|
|
|
+ if(!is_array($value) || count($value) == 0) {
|
|
|
+ return Response::FailUpdate('_incr', array('_result' => '未指定incr的key'));
|
|
|
+ }
|
|
|
+ $incrFields = array();
|
|
|
+ $privateKeys = $this->getPrivateKey();
|
|
|
+ foreach ($value as $key => $step) {
|
|
|
+ if($step <= 0) continue;
|
|
|
+ //去掉private的值
|
|
|
+ if(in_array($key, $privateKeys)) continue;
|
|
|
+ $incrFields[$key .':plus'] = $step;
|
|
|
+ }
|
|
|
+ if(count($incrFields) == 0){
|
|
|
+ return Response::FailUpdate('_incr', array('_result' => '未指定incr的步长'));
|
|
|
+ }
|
|
|
+
|
|
|
+ $fieldsAndValues = $this->getWhereCondition();
|
|
|
+
|
|
|
+ $result = $this->_easyORM->db
|
|
|
+ ->set($incrFields)
|
|
|
+ ->where($fieldsAndValues)
|
|
|
+ ->like($this->_easyORM->like)
|
|
|
+ ->exclude($this->_easyORM->exclude)
|
|
|
+ ->update($this->_easyORM->rules->getTable());
|
|
|
+
|
|
|
+ if ($this->_easyORM->db->isError()) {
|
|
|
+ return $this->_easyORM->db->getResponse();
|
|
|
+ }
|
|
|
+ return Response::Success('_incr', array('_result' => $result));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 清除limit限制
|
|
|
- * @return $this
|
|
|
+ * 更新数据只验证传过来的参数是否正确
|
|
|
+ *
|
|
|
+ * @return mixed|Qii\Driver\Response
|
|
|
*/
|
|
|
- public function cleanLimit() {
|
|
|
- $this->_easyORM->limit = array();
|
|
|
- return $this;
|
|
|
+ final public function updateFields() {
|
|
|
+ $values = $this->_easyORM->fields->getValueAsArray();
|
|
|
+ $this->_easyORM->rules->addForceValidKey(array_keys($values));
|
|
|
+ $this->_easyORM->rules->addValues($values);
|
|
|
+ $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']), 'message' => $verify['msg']));
|
|
|
+ }
|
|
|
+ if (($this->_easyORM->privateKeys || $this->_easyORM->exclude) && $this->exist()->count() == 0) {
|
|
|
+ return Response::NotExist('update', array('_result' => \Qii::i(1512, join(',', $this->getPrivateValue()))));
|
|
|
+ }
|
|
|
+ $where = $this->getWhereCondition();
|
|
|
+ //merge like 字段
|
|
|
+ if($this->_easyORM->like) {
|
|
|
+ $where = array_merge($this->_easyORM->like, $where);
|
|
|
+ }
|
|
|
+ $map = $this->_easyORM->rules->getValues();
|
|
|
+ $result = $this->_easyORM->db->updateObject($this->_easyORM->rules->getTable(), $map, $where);
|
|
|
+ if ($this->_easyORM->db->isError()) {
|
|
|
+ return $this->_easyORM->db->getResponse();
|
|
|
+ }
|
|
|
+ return Response::Success('_update', array('_result' => $result));
|
|
|
}
|
|
|
/**
|
|
|
* 返回所有数据
|
|
|
*
|
|
|
* @return mixed
|
|
|
*/
|
|
|
- public function lists() {
|
|
|
+ final public function lists() {
|
|
|
if($this->_easyORM->limit && count($this->_easyORM->limit) > 0) {
|
|
|
$this->_easyORM->db->limit($this->_easyORM->limit[0], $this->_easyORM->limit[1]);
|
|
|
}
|
|
|
- $fieldsAndValues = $this->getValueAsArray();
|
|
|
- if($this->_easyORM->where && is_array($this->_easyORM->where)) {
|
|
|
- $fieldsAndValues = array_merge($fieldsAndValues, $this->_easyORM->where);
|
|
|
+ $fieldsAndValues = $this->getWhereCondition();
|
|
|
+ //字段值作为where条件, 仅list和count用
|
|
|
+ $value = $this->getValueAsArray();
|
|
|
+ foreach ($value as $key => $val) {
|
|
|
+ $fieldsAndValues[$key] = $val;
|
|
|
}
|
|
|
- return $this->_easyORM->db
|
|
|
+ $result = $this->_easyORM->db
|
|
|
->fields($this->_easyORM->queryFields)
|
|
|
->where($fieldsAndValues)
|
|
|
+ ->like($this->_easyORM->like)
|
|
|
->exclude($this->_easyORM->exclude)
|
|
|
+ ->orderBy($this->_easyORM->orderBy)
|
|
|
->selectRows($this->getTable());
|
|
|
+ if ($this->_easyORM->db->isError()) {
|
|
|
+ return $this->_easyORM->db->getResponse();
|
|
|
+ }
|
|
|
+ return Response::Success('lists', array('_result' => $result));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置order by
|
|
|
+ * @param $orderBy
|
|
|
+ * @return EasyORM
|
|
|
+ */
|
|
|
+ final public function setOrderBy($orderBy) {
|
|
|
+ if(!$orderBy) {
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+ if(!is_array($orderBy)) {
|
|
|
+ $this->_easyORM->orderBy[] = $orderBy;
|
|
|
+ }else{
|
|
|
+ $this->_easyORM->orderBy = array_merge($this->_easyORM->orderBy, $orderBy);
|
|
|
+ }
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 清除order by 字段
|
|
|
+ *
|
|
|
+ * @return EasyORM
|
|
|
+ */
|
|
|
+ public function cleanOrderBy() {
|
|
|
+ $this->_easyORM->orderBy = [];
|
|
|
+ return $this;
|
|
|
}
|
|
|
/**
|
|
|
* 删除指定数据
|
|
@@ -460,37 +635,41 @@ final class EasyORM {
|
|
|
* @return Qii\Driver\Response
|
|
|
*/
|
|
|
final public function remove() {
|
|
|
- if(!$this->_easyORM->privateKeys && !$this->_easyORM->exclude) {
|
|
|
+ if(!$this->_easyORM->privateKeys && !$this->_easyORM->exclude && !$this->_easyORM->where) {
|
|
|
return Response::FAIL('privateKey', \Qii::i(1513));
|
|
|
}
|
|
|
- $fieldsAndValues = [];
|
|
|
- foreach ($this->_easyORM->privateKeys as $key) {
|
|
|
- $fieldsAndValues[$key] = $this->_easyORM->fields->getField($key);
|
|
|
+ $fieldsAndValues = $this->getWhereCondition();
|
|
|
+
|
|
|
+ //检查数据是否存在
|
|
|
+ if($this->exist()->count() == 0) {
|
|
|
+ return Response::NotExist('update', array('_result' => \Qii::i(1512, join(',', $this->getPrivateValue()))));
|
|
|
}
|
|
|
+
|
|
|
$result = $this->_easyORM->db
|
|
|
->where($fieldsAndValues)
|
|
|
+ ->like($this->_easyORM->like)
|
|
|
->exclude($this->_easyORM->exclude)
|
|
|
->remove($this->_easyORM->rules->getTable());
|
|
|
+ if ($this->_easyORM->db->isError()) {
|
|
|
+ return $this->_easyORM->db->getResponse();
|
|
|
+ }
|
|
|
return Response::Success('remove', array('_result' => $result));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 检查数据是否已经存在,并返回一行,只能根据主键查询
|
|
|
+ * privateKey 与 exclude、where 条件互斥
|
|
|
*
|
|
|
* @return \Qii\Driver\Response
|
|
|
*/
|
|
|
final public function exist() {
|
|
|
- if(!$this->_easyORM->privateKeys) {
|
|
|
+ if(!$this->_easyORM->privateKeys && !$this->_easyORM->exclude && !$this->_easyORM->where) {
|
|
|
$this->setPrivateKey(array_keys($this->getValueAsArray()));
|
|
|
}
|
|
|
- if(!$this->_easyORM->privateKeys && !$this->_easyORM->exclude) {
|
|
|
+ if(!$this->_easyORM->privateKeys && !$this->_easyORM->exclude && !$this->_easyORM->where) {
|
|
|
return Response::FAIL('privateKey', \Qii::i(1513));
|
|
|
}
|
|
|
- $fieldsAndValues = [];
|
|
|
- foreach ($this->_easyORM->privateKeys as $key) {
|
|
|
- $fieldsAndValues[$key] = $this->_easyORM->fields->getField($key);
|
|
|
- }
|
|
|
-
|
|
|
+ $fieldsAndValues = $this->getWhereCondition();
|
|
|
$verify = $this->_easyORM->rules->verifyFields($fieldsAndValues);
|
|
|
if(!$verify['valid']) {
|
|
|
return Response::FailValidate('validate', array('_result' => \Qii::i($verify['code'], $verify['msg'])
|
|
@@ -505,11 +684,31 @@ final class EasyORM {
|
|
|
$result = $this->_easyORM->db->limit(1)
|
|
|
->fields($this->_easyORM->queryFields)
|
|
|
->where($fieldsAndValues)
|
|
|
+ ->like($this->_easyORM->like)
|
|
|
->exclude($this->_easyORM->exclude)
|
|
|
+ ->orderBy($this->_easyORM->orderBy)
|
|
|
->selectRow($this->_easyORM->rules->getTable());
|
|
|
+ if ($this->_easyORM->db->isError()) {
|
|
|
+ return $this->_easyORM->db->getResponse();
|
|
|
+ }
|
|
|
return Response::Success('exist', array('_result' => $result));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 组合where查询条件
|
|
|
+ *
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ final public function getWhereCondition() {
|
|
|
+ $fieldsAndValues = $this->getPrivateValue();
|
|
|
+ //merge where 条件的,这里不用merge,因为merge可能会merge出一个0索引的值
|
|
|
+ if($this->_easyORM->where && is_array($this->_easyORM->where)) {
|
|
|
+ foreach ($this->_easyORM->where as $key => $value) {
|
|
|
+ $fieldsAndValues[$key] = $value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $fieldsAndValues;
|
|
|
+ }
|
|
|
/**
|
|
|
* 使用此方法用于查询某一条数据的某一个字段
|
|
|
* @useage getXxxByXxx _getXxxByxxx
|