|
@@ -12,9 +12,25 @@ class Base {
|
|
|
* @var array $properties
|
|
|
*/
|
|
|
public static $properties;
|
|
|
-
|
|
|
+ /**
|
|
|
+ * 配置信息
|
|
|
+ *
|
|
|
+ * @var array $config
|
|
|
+ */
|
|
|
public static $config;
|
|
|
/**
|
|
|
+ * 唯一字段
|
|
|
+ *
|
|
|
+ * @var array $uniqKeys
|
|
|
+ */
|
|
|
+ public static $uniqKeys = null;
|
|
|
+ /**
|
|
|
+ * 设置排除项
|
|
|
+ *
|
|
|
+ * @var array $exclude
|
|
|
+ */
|
|
|
+ public static $exclude = [];
|
|
|
+ /**
|
|
|
* @var string 表别名
|
|
|
*/
|
|
|
private $alias;
|
|
@@ -22,7 +38,7 @@ class Base {
|
|
|
* hooker for fields 查询字段/ where 查询条件 / order 排序 / cache 缓存 / with 关联查询
|
|
|
* @var array $hooker
|
|
|
*/
|
|
|
- private $hooker = [];
|
|
|
+ private $hooker = array();
|
|
|
/**
|
|
|
* 默认过期时间
|
|
|
*
|
|
@@ -190,24 +206,26 @@ class Base {
|
|
|
/**
|
|
|
* 设置order by hooker
|
|
|
* @param object $hooker
|
|
|
- * @return void
|
|
|
+ * @return $this
|
|
|
* @throws \Exception
|
|
|
*/
|
|
|
public function setOrderHooker($hooker, $args = null) {
|
|
|
$this->checkCallable($hooker);
|
|
|
$this->setHooker('order', $hooker, $args);
|
|
|
+ return $this;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置 limit hooker
|
|
|
*
|
|
|
* @param object $hooker
|
|
|
- * @return void
|
|
|
+ * @return $this
|
|
|
* @throws \Exception
|
|
|
*/
|
|
|
public function setLimitHooker($hooker, $args = null) {
|
|
|
$this->checkCallable($hooker);
|
|
|
$this->setHooker('order', $hooker, $args);
|
|
|
+ return $this;
|
|
|
}
|
|
|
/**
|
|
|
* 获取limit
|
|
@@ -225,12 +243,13 @@ class Base {
|
|
|
*
|
|
|
* @param callable $hooker
|
|
|
* @param array| mixed $args
|
|
|
- * @return void
|
|
|
+ * @return $this
|
|
|
* @throws \Exception
|
|
|
*/
|
|
|
public function setCacheHooker($hooker, $args = null) {
|
|
|
$this->checkCallable($hooker);
|
|
|
$this->setHooker('cache', $hooker, $args);
|
|
|
+ return $this;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -376,6 +395,16 @@ class Base {
|
|
|
{
|
|
|
return [];
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置唯一字段
|
|
|
+ *
|
|
|
+ * @param mixed $uniqKeys 唯一字段
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function setUniqKeys($uniqKeys) {
|
|
|
+ $this->uniqKeys = $uniqKeys;
|
|
|
+ }
|
|
|
/**
|
|
|
* unique (unique 如果是 array 则表示 联合唯一,如果是string则是任意唯一,多个单独唯一使用字符串以逗号隔开, 主键除外)
|
|
|
*
|
|
@@ -383,6 +412,9 @@ class Base {
|
|
|
* @throws \Exception
|
|
|
*/
|
|
|
public function uniqueKey(){
|
|
|
+ if(self::$uniqKeys !== null) {
|
|
|
+ return self::$uniqKeys;
|
|
|
+ }
|
|
|
throw new \Exception('请设置唯一值');
|
|
|
}
|
|
|
|
|
@@ -397,11 +429,25 @@ class Base {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 保存数据的时候,验证唯一需要排除的值,此处仅支持,单个或联合排除,不支持单个排除
|
|
|
+ * 设置排除项
|
|
|
+ * @param array $exclude
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function setExclude($exclude) {
|
|
|
+ if(!is_array($exclude)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ self::$exclude = $exclude;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 保存数据的时候,验证唯一需要排除的值,此处仅支持,单个或联合排除,不支持单个异或排除
|
|
|
*
|
|
|
* @return array
|
|
|
*/
|
|
|
public function exclude(){
|
|
|
+ if(self::$exclude !== null) {
|
|
|
+ return self::$exclude;
|
|
|
+ }
|
|
|
return array();
|
|
|
}
|
|
|
|
|
@@ -614,7 +660,7 @@ class Base {
|
|
|
*
|
|
|
* @return array
|
|
|
*/
|
|
|
- public function toArray()
|
|
|
+ public function toArray($returnFields = false)
|
|
|
{
|
|
|
$arr = [];
|
|
|
$method = new \ReflectionClass($this);
|
|
@@ -626,8 +672,10 @@ class Base {
|
|
|
}
|
|
|
$fields = $this->fields();
|
|
|
if(count($fields)) {
|
|
|
- foreach ($fields as $key => $value) {
|
|
|
- $arr[$key] = $value;
|
|
|
+ if($returnFields) {
|
|
|
+ foreach ($fields as $key => $value) {
|
|
|
+ $arr['__fields__'][$key] = $value;
|
|
|
+ }
|
|
|
}
|
|
|
foreach ($vars as $extra) {
|
|
|
if(!isset($arr[$extra])) {
|
|
@@ -1204,9 +1252,11 @@ class Base {
|
|
|
if(count($this->getFields()) > 0 && !in_array($relKey, $this->getFields())) {
|
|
|
$fields[] = $relKey;
|
|
|
}
|
|
|
+ if(array_search('*', $fields) !== false) {
|
|
|
+ $fields = ['*'];
|
|
|
+ }
|
|
|
$query = $this->createQuery($fields);
|
|
|
$res = $query->where($args[1])->rs($this->prepareTable());
|
|
|
- echo $this->calledSQL;
|
|
|
$rows = [];
|
|
|
foreach($res as $v) {
|
|
|
$rows[$v[$relKey]][] = $v;
|