|
@@ -19,6 +19,11 @@ class Base {
|
|
|
*/
|
|
|
public static $config;
|
|
|
/**
|
|
|
+ * 主键
|
|
|
+ * @var mix $privateKeys null
|
|
|
+ */
|
|
|
+ public static $privateKeys = null;
|
|
|
+ /**
|
|
|
* 唯一字段
|
|
|
*
|
|
|
* @var array $uniqKeys
|
|
@@ -34,6 +39,8 @@ class Base {
|
|
|
* @var string 表别名
|
|
|
*/
|
|
|
private $alias;
|
|
|
+
|
|
|
+ private $nullFields = [];
|
|
|
/**
|
|
|
* hooker for fields 查询字段/ where 查询条件 / order 排序 / cache 缓存 / with 关联查询
|
|
|
* @var array $hooker
|
|
@@ -419,12 +426,24 @@ class Base {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 设置主键
|
|
|
+ *
|
|
|
+ * @param mixed $privateKeys
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function setPricateKey($privateKeys){
|
|
|
+ self::$privateKeys = $privateKeys;
|
|
|
+ }
|
|
|
+ /**
|
|
|
* 主键
|
|
|
*
|
|
|
* @return mixed
|
|
|
* @throws \Exception
|
|
|
*/
|
|
|
public function primaryKey(){
|
|
|
+ if(self::$privateKeys !== null) {
|
|
|
+ return self::$privateKeys;
|
|
|
+ }
|
|
|
throw new \Exception('请设置主键');
|
|
|
}
|
|
|
|
|
@@ -499,6 +518,25 @@ class Base {
|
|
|
public function validFieldsForUpdate() {
|
|
|
throw new \Exception('请设置更新数据验证的字段,格式如:["Id", "Title"],Id和Title为entity的属性');
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置需要更新为空的字段列表
|
|
|
+ *
|
|
|
+ * @param $fields
|
|
|
+ * @return $this
|
|
|
+ */
|
|
|
+ public function setNull($fields) {
|
|
|
+ $tableFields = $this->fields();
|
|
|
+ foreach ($fields AS $field) {
|
|
|
+ if(!in_array($field, $tableFields)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ $field = $this->entity()->convertToProperty($field);
|
|
|
+ $this->nullFields[] = $field;
|
|
|
+ }
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取不为空的属性
|
|
|
*
|
|
@@ -519,6 +557,12 @@ class Base {
|
|
|
$fields[$field] = $this->$name;
|
|
|
}
|
|
|
}
|
|
|
+ if(count($this->nullFields) > 0) {
|
|
|
+ foreach ($this->nullFields AS $field) {
|
|
|
+ $field = $this->entity()->convertToField($field);
|
|
|
+ $fields[$field] = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
return $fields;
|
|
|
}
|
|
|
|
|
@@ -616,7 +660,7 @@ class Base {
|
|
|
$obj = new $class();
|
|
|
$response = $this->info();
|
|
|
if($response->isError()) {
|
|
|
- $obj->Error = $response->getResult();
|
|
|
+ $obj->__Error = $response->getResult();
|
|
|
return $obj;
|
|
|
}
|
|
|
$info = $response->getResult()['body'];
|
|
@@ -630,6 +674,42 @@ class Base {
|
|
|
}
|
|
|
return $obj;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * get 是否返回了错误
|
|
|
+ *
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function isError() {
|
|
|
+ if(isset($this->__Error)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * get 返回的code
|
|
|
+ *
|
|
|
+ * @return int
|
|
|
+ */
|
|
|
+ public function getCode() {
|
|
|
+ if(!$this->isError()) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ return isset($this->__Error['code']) ? $this->__Error['code'] : 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * get 返回的msg
|
|
|
+ *
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public function getMessage() {
|
|
|
+ if(!$this->isError()) {
|
|
|
+ return '';
|
|
|
+ }
|
|
|
+ return isset($this->__Error['msg']) ? $this->__Error['msg'] : '';
|
|
|
+ }
|
|
|
/**
|
|
|
* 与get不同的这个返回的是Response
|
|
|
*
|
|
@@ -870,7 +950,6 @@ class Base {
|
|
|
//获取默认值
|
|
|
//更新的时候不使用默认值
|
|
|
//$values = array_merge($this->getDefaultValue(), $this->properties());
|
|
|
-
|
|
|
$affectedRows = $this->db()->updateObject($this->prepareTable(), $this->properties(), $primaryKey);
|
|
|
if($this->db()->isError()) {
|
|
|
return Response::FailUpdate(static::class .'::'. __FUNCTION__,
|
|
@@ -1148,6 +1227,22 @@ class Base {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回游标
|
|
|
+ *
|
|
|
+ * @param int | null $page 页码
|
|
|
+ * @param int | null $pageSize 页大小
|
|
|
+ * @return mixed
|
|
|
+ * @throws \Qii\Exceptions\InvalidParams
|
|
|
+ */
|
|
|
+ public function rs($page = null, $pageSize = null) {
|
|
|
+ $query = $this->createQuery()->orderBy($this->getOrderBy());
|
|
|
+ if($page && $pageSize) {
|
|
|
+ $query->limit($page, $pageSize);
|
|
|
+ }
|
|
|
+ return $query->rs($this->prepareTable());
|
|
|
+ }
|
|
|
/**
|
|
|
* listts
|
|
|
* @param int $page 页码
|
|
@@ -1603,6 +1698,32 @@ class Base {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 开始事务
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function startTrans() {
|
|
|
+ return $this->db()->transaction();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 提交事务
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function commit() {
|
|
|
+ return $this->db()->commit();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 回退
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function rollback() {
|
|
|
+ return $this->db()->rollback();
|
|
|
+ }
|
|
|
+ /**
|
|
|
* 验证规则,由子类继承去修改
|
|
|
*
|
|
|
* @return array
|