|
@@ -1,8 +1,5 @@
|
|
|
<?php
|
|
|
namespace Qii\Driver\Entity;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
use Qii\Driver\Model;
|
|
|
use Qii\Driver\Response;
|
|
|
use Qii\Exceptions\InvalidParams;
|
|
@@ -321,6 +318,39 @@ class Base {
|
|
|
}
|
|
|
return $join;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置group
|
|
|
+ *
|
|
|
+ * @param array $group group对应的数组
|
|
|
+ * @param mixed $args
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ final public function group($group, $args = null) {
|
|
|
+ if(!isset($this->hooker['group'])) {
|
|
|
+ $this->hooker['group'] = [];
|
|
|
+ }
|
|
|
+ $this->hooker['group'][] = ['func' => function ()use ($group) {
|
|
|
+ return $group;
|
|
|
+ }, 'args' => $args];
|
|
|
+
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * get group hooker
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ final public function getGroupBy() {
|
|
|
+ if(!isset($this->hooker['group']) || !is_array($this->hooker['group'])) {
|
|
|
+ return [];
|
|
|
+ }
|
|
|
+ $group = [];
|
|
|
+ foreach ($this->hooker['group'] as $value) {
|
|
|
+ $group = array_merge(call_user_func($value['func'], [], $value['args']));
|
|
|
+ }
|
|
|
+ return $group;
|
|
|
+ }
|
|
|
/**
|
|
|
* order by
|
|
|
* @param array $order
|
|
@@ -711,6 +741,19 @@ class Base {
|
|
|
throw new \Exception('请设置更新数据验证的字段,格式如:["Id", "Title"],Id和Title为entity的属性');
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新数据验证字段
|
|
|
+ *
|
|
|
+ * $fields = [];
|
|
|
+ * return $this->valid($fields);
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ public function validFieldsForReplace() {
|
|
|
+ throw new \Exception('请设置更新数据验证的字段,格式如:["Id", "Title"],Id和Title为entity的属性');
|
|
|
+ }
|
|
|
/**
|
|
|
* 设置需要更新为空的字段列表
|
|
|
*
|
|
@@ -785,6 +828,7 @@ class Base {
|
|
|
public function count() {
|
|
|
$query = $this->createQuery();
|
|
|
$query->fields(' COUNT(1) as count');
|
|
|
+ $query->groupBy($this->getGroupBy());
|
|
|
return $query->selectOne($this->prepareTable());
|
|
|
}
|
|
|
/**
|
|
@@ -1026,7 +1070,7 @@ class Base {
|
|
|
* @throws InvalidParams
|
|
|
*/
|
|
|
final public function replace(){
|
|
|
- $valid = $this->validFieldsForAdd();
|
|
|
+ $valid = $this->validFieldsForReplace();
|
|
|
if($valid->isError()) {
|
|
|
return $valid;
|
|
|
}
|
|
@@ -1072,8 +1116,8 @@ class Base {
|
|
|
if($exist) {
|
|
|
return Response::Exist(static::class .'::'. __FUNCTION__,
|
|
|
[
|
|
|
- '_result' => ['code' => Response::DOES_EXIST, 'msg' => '数据已经存在', 'body' => $exist],
|
|
|
- 'message' => '数据已经存在'
|
|
|
+ '_result' => ['code' => Response::DOES_EXIST, 'msg' => '数据已经存在(1)', 'body' => $exist],
|
|
|
+ 'message' => '数据已经存在(1)'
|
|
|
]
|
|
|
);
|
|
|
}
|
|
@@ -1084,8 +1128,8 @@ class Base {
|
|
|
if($exist) {
|
|
|
return Response::Exist(static::class .'::'. __FUNCTION__,
|
|
|
[
|
|
|
- '_result' => ['code' => Response::DOES_EXIST, 'msg' => '数据已经存在', 'body' => $exist],
|
|
|
- 'message' => '数据已存在'
|
|
|
+ '_result' => ['code' => Response::DOES_EXIST, 'msg' => '数据已经存在(2)', 'body' => $exist],
|
|
|
+ 'message' => '数据已存在(2)'
|
|
|
]
|
|
|
);
|
|
|
}
|
|
@@ -1097,8 +1141,8 @@ class Base {
|
|
|
if($this->db()->isError()) {
|
|
|
return Response::FailSave(static::class .'::'. __FUNCTION__,
|
|
|
[
|
|
|
- '_result' => ['code' => Response::FAIL_FOR_SAVE, 'msg' => $this->db()->getMessage(), 'body' => []],
|
|
|
- 'message' => $this->db()->getMessage()
|
|
|
+ '_result' => ['code' => Response::FAIL_FOR_SAVE, 'msg' => $this->db()->getMessage() . "(3)", 'body' => []],
|
|
|
+ 'message' => $this->db()->getMessage() . "(3)"
|
|
|
]);
|
|
|
}
|
|
|
return Response::Success(static::class .'::'. __FUNCTION__,
|
|
@@ -1313,8 +1357,10 @@ class Base {
|
|
|
unset($where, $or, $exclude);
|
|
|
$property = $this->properties();
|
|
|
$incr = [];
|
|
|
+ $sets = [];
|
|
|
foreach ($property as $key => $value) {
|
|
|
if(!is_numeric($value)) {
|
|
|
+ $sets[$key] = $value;
|
|
|
continue;
|
|
|
}
|
|
|
$incr[$key] = $value;
|
|
@@ -1328,7 +1374,6 @@ class Base {
|
|
|
]
|
|
|
);
|
|
|
}
|
|
|
- $sets = [];
|
|
|
foreach ($diff as $key => $val) {
|
|
|
if($val == 0) {
|
|
|
$sets[$key] = $val;
|
|
@@ -1399,7 +1444,7 @@ class Base {
|
|
|
$orderBy[$key] = 'ASC';
|
|
|
}
|
|
|
$query = $this->createQuery();
|
|
|
- $row = $query->limit(1)->orderBy($orderBy)->selectRow($this->prepareTable());
|
|
|
+ $row = $query->limit(1)->groupBy($this->getGroupBy())->orderBy($orderBy)->selectRow($this->prepareTable());
|
|
|
if($this->db()->isError()) {
|
|
|
return Response::Fail(static::class .'::'. __FUNCTION__,
|
|
|
[
|
|
@@ -1429,7 +1474,7 @@ class Base {
|
|
|
$orderBy[$key] = 'DESC';
|
|
|
}
|
|
|
$query = $this->createQuery();
|
|
|
- $row = $query->orderBy($orderBy)->limit(1)->selectRow($this->prepareTable());
|
|
|
+ $row = $query->groupBy($this->getGroupBy())->orderBy($orderBy)->limit(1)->selectRow($this->prepareTable());
|
|
|
$this->calledSQL = $query->modelSQL;
|
|
|
if($this->db()->isError()) {
|
|
|
return Response::Fail(static::class .'::'. __FUNCTION__,
|
|
@@ -1499,7 +1544,7 @@ class Base {
|
|
|
* @throws \Qii\Exceptions\InvalidParams
|
|
|
*/
|
|
|
final public function rs($page = null, $pageSize = null) {
|
|
|
- $query = $this->createQuery()->orderBy($this->getOrderBy());
|
|
|
+ $query = $this->createQuery()->groupBy($this->getGroupBy())->orderBy($this->getOrderBy());
|
|
|
if($page && $pageSize) {
|
|
|
$start = ((max($page, 1)) - 1) * $pageSize;
|
|
|
$query->limit($start, $pageSize);
|
|
@@ -1521,6 +1566,7 @@ class Base {
|
|
|
$pageSize = isset($limit[1]) ? $limit[1] : null;
|
|
|
}
|
|
|
$query = $this->createQuery();
|
|
|
+ $query = $query->groupBy($this->getGroupBy());
|
|
|
$query = $query->orderBy($this->getOrderBy());
|
|
|
if($page !== null) {
|
|
|
$query = $query->limit($page, $pageSize);
|
|
@@ -1672,7 +1718,7 @@ class Base {
|
|
|
*/
|
|
|
final public function listAll() {
|
|
|
$limit = $this->getLimitHooker();
|
|
|
- $query = $this->createQuery()->orderBy($this->getOrderBy());
|
|
|
+ $query = $this->createQuery()->groupBy($this->getGroupBy())->orderBy($this->getOrderBy());
|
|
|
if(empty($limit) || !is_array($limit)) {
|
|
|
$list = $query
|
|
|
->selectRows($this->prepareTable());
|