shish 5 ani în urmă
părinte
comite
6db5d64c3e
1 a modificat fișierele cu 330 adăugiri și 329 ștergeri
  1. 330 329
      common/base/models/Base.php

+ 330 - 329
common/base/models/Base.php

@@ -1,4 +1,5 @@
 <?php
+
 namespace common\base\models;
 
 use common\components\util;
@@ -7,335 +8,335 @@ use yii\db\ActiveRecord;
 
 class Base extends ActiveRecord
 {
-	
-	public function getModel()
-	{
-		$self = get_called_class();
-		return new $self;
-	}
-	
-	/**
-	 * 添加
-	 */
-	public function add($data, $returnObject = false)
-	{
-		$model = $this->getModel();
-		$attributes = $model->attributes;
-		$fields = array_keys($attributes);
-		foreach ($fields as $key) {
-			if (isset($data[$key])) {
-				$model->$key = $data[$key];
-			}
-		}
-		$model->save();
-		if ($returnObject) {
-			return $model;
-		}
-		return $model->attributes;
-	}
-	
-	/**
-	 * 批量添加
-	 */
-	public function batchAdd($data)
-	{
-		$model = $this->getModel();
-		foreach ($data as $attributes) {
-			$newModel = clone $model;
-			// false 不检测字段安全 true 需要在model设置rules
-			$newModel->setAttributes($attributes, false);
-			$newModel->save();
-		}
-	}
-	
-	/**
-	 * 取表的字段
-	 */
-	public function getFields()
-	{
-		$model = $this->getModel();
-		$attributes = $model->attributes;
-		return array_keys($attributes);
-	}
-	
-	/**
-	 * 根据主键ID删除一条记录
-	 */
-	public function deleteById($id)
-	{
-		//shizhongqi 2019-12-07
-		$model = $this->getModel();
-		return $model::findOne($id)->delete();
-	}
-	
-	//根据id批量删 ssh 2019.12.7
-	public function deleteByIds($ids)
-	{
-		$model = $this->getModel();
-		$model::deleteAll(['id' => $ids]);
-	}
-	
-	/**
-	 *根据条件删除一条或多条记录
-	 */
-	public function deleteByCondition($condition)
-	{
-		if (empty($condition)) {
-			return 0;
-		}
-		$where = array();
-		$variable = array();
-		foreach ($condition as $key => $val) {
-			$where[] = $key . '=:' . $key;
-			$variable[':' . $key] = $val;
-		}
-		$whereString = implode(' and ', $where);
-		$count = self::deleteAll($whereString, $variable);
-		return $count;
-	}
-	
-	
-	/**
-	 * 根据主键ID更新一条记录
-	 */
-	public function updateById($id, $data)
-	{
-		$model = $this->getModel();
-		$attributes = $model->attributes;
-		$fields = array_keys($attributes);
-		foreach ($data as $key => $val) {
-			if (in_array($key, $fields) == false) {
-				//删除表里不存在字段
-				unset($data[$key]);
-			}
-		}
-		$re = self::updateAll($data, 'id=:id', ['id' => $id]);
-		if ($re == 1) {
-			return ['status' => true, 'data' => $data];
-		}
-		return ['status' => false, 'data' => $data];
-	}
-	
-	//多个主键更新 ssh 2019.9.3
-	public function updateByIds($ids, $data)
-	{
-		self::updateAll($data, ['id' => $ids]);
-	}
-	
-	/**
-	 * 根据条件更新一条或多条记录
-	 */
-	public function updateByCondition($condition, $data)
-	{
-		$where = array();
-		$variable = array();
-		foreach ($condition as $key => $val) {
-			$where[] = $key . '=:' . $key;
-			$variable[':' . $key] = $val;
-		}
-		$whereString = implode(' and ', $where);
-		$count = self::updateAll($data, $whereString, $variable);
-		return $count;
-	}
-	
-	/**
-	 * 根据主键ID查出一条记录
-	 */
-	public function getById($id, $returnObject = false)
-	{
-		if ($returnObject == false) {
-			return $this->conditionQuery(['id' => $id])->asArray()->one();
-		} else {
-			return $this->conditionQuery(['id' => $id])->one();
-		}
-	}
-	
-	/**
-	 *根据条件查出一条记录
-	 */
-	public function getByCondition($condition, $returnObject = false, $order = false)
-	{
-		$query = $this->conditionQuery($condition);
-		if (isset($order)) {
-			$query->orderBy($order);
-		}
-		return $returnObject == true ? $query->one() : $query->asArray()->one();
-	}
 
-	//随机取一个 ssh 2020.2.10
-	public function getOne($returnObject = false, $order = false)
-	{
-		$query = $this->conditionQuery([]);
-		if (isset($order)) {
-			$query->orderBy($order);
-		}
-		return $returnObject == true ? $query->one() : $query->asArray()->one();
-	}
+    public function getModel()
+    {
+        $self = get_called_class();
+        return new $self;
+    }
+
+    /**
+     * 添加
+     */
+    public function add($data, $returnObject = false)
+    {
+        $model = $this->getModel();
+        $attributes = $model->attributes;
+        $fields = array_keys($attributes);
+        foreach ($fields as $key) {
+            if (isset($data[$key])) {
+                $model->$key = $data[$key];
+            }
+        }
+        $model->save();
+        if ($returnObject) {
+            return $model;
+        }
+        return $model->attributes;
+    }
+
+    /**
+     * 批量添加
+     */
+    public function batchAdd($data)
+    {
+        $model = $this->getModel();
+        foreach ($data as $attributes) {
+            $newModel = clone $model;
+            // false 不检测字段安全 true 需要在model设置rules
+            $newModel->setAttributes($attributes, false);
+            $newModel->save();
+        }
+    }
+
+    /**
+     * 取表的字段
+     */
+    public function getFields()
+    {
+        $model = $this->getModel();
+        $attributes = $model->attributes;
+        return array_keys($attributes);
+    }
+
+    /**
+     * 根据主键ID删除一条记录
+     */
+    public function deleteById($id)
+    {
+        //shizhongqi 2019-12-07
+        $model = $this->getModel();
+        return $model::findOne($id)->delete();
+    }
+
+    //根据id批量删 ssh 2019.12.7
+    public function deleteByIds($ids)
+    {
+        $model = $this->getModel();
+        $model::deleteAll(['id' => $ids]);
+    }
+
+    /**
+     *根据条件删除一条或多条记录
+     */
+    public function deleteByCondition($condition)
+    {
+        if (empty($condition)) {
+            return 0;
+        }
+        $where = array();
+        $variable = array();
+        foreach ($condition as $key => $val) {
+            $where[] = $key . '=:' . $key;
+            $variable[':' . $key] = $val;
+        }
+        $whereString = implode(' and ', $where);
+        $count = self::deleteAll($whereString, $variable);
+        return $count;
+    }
+
+
+    /**
+     * 根据主键ID更新一条记录
+     */
+    public function updateById($id, $data)
+    {
+        $model = $this->getModel();
+        $attributes = $model->attributes;
+        $fields = array_keys($attributes);
+        foreach ($data as $key => $val) {
+            if (in_array($key, $fields) == false) {
+                //删除表里不存在字段
+                unset($data[$key]);
+            }
+        }
+        $re = self::updateAll($data, 'id=:id', ['id' => $id]);
+        if ($re == 1) {
+            return ['status' => true, 'data' => $data];
+        }
+        return ['status' => false, 'data' => $data];
+    }
+
+    //多个主键更新 ssh 2019.9.3
+    public function updateByIds($ids, $data)
+    {
+        self::updateAll($data, ['id' => $ids]);
+    }
+
+    /**
+     * 根据条件更新一条或多条记录
+     */
+    public function updateByCondition($condition, $data)
+    {
+        $where = array();
+        $variable = array();
+        foreach ($condition as $key => $val) {
+            $where[] = $key . '=:' . $key;
+            $variable[':' . $key] = $val;
+        }
+        $whereString = implode(' and ', $where);
+        $count = self::updateAll($data, $whereString, $variable);
+        return $count;
+    }
+
+    /**
+     * 根据主键ID查出一条记录
+     */
+    public function getById($id, $returnObject = false)
+    {
+        if ($returnObject == false) {
+            return $this->conditionQuery(['id' => $id])->asArray()->one();
+        } else {
+            return $this->conditionQuery(['id' => $id])->one();
+        }
+    }
+
+    /**
+     *根据条件查出一条记录
+     */
+    public function getByCondition($condition, $returnObject = false, $order = false)
+    {
+        $query = $this->conditionQuery($condition);
+        if (isset($order)) {
+            $query->orderBy($order);
+        }
+        return $returnObject == true ? $query->one() : $query->asArray()->one();
+    }
+
+    //随机取一个 ssh 2020.2.10
+    public function getOne($returnObject = false, $order = false)
+    {
+        $query = $this->conditionQuery([]);
+        if (isset($order)) {
+            $query->orderBy($order);
+        }
+        return $returnObject == true ? $query->one() : $query->asArray()->one();
+    }
+
+    /**
+     * 支持查询条件包括
+     * ['name'=>'john','id>'=>3,'id<'=>10,'id'=>['in',[10,11,12]],'age'=>['between',[20,30]],'name'=>['like','Jack']];
+     */
+    public function conditionQuery($condition = [])
+    {
+        $whereNum = 0;
+        $query = self::find();
+        if (!empty($condition)) {
+            foreach ($condition as $key => $val) {
+                $param = [$key => $val];
+                if (strstr($key, '>')) {
+                    //暂不支持大于等于,等于请使用between
+                    $pos = strpos($key, '>');
+                    $currentKey = substr($key, 0, $pos);
+                    $param = ['>', $currentKey, $val];
+                } elseif (strstr($key, '<')) {
+                    //暂不支持小于等于,等于请使用between
+                    $pos = strpos($key, '<');
+                    $currentKey = substr($key, 0, $pos);
+                    $param = ['<', $currentKey, $val];
+                } elseif (is_array($val)) {
+                    if (count($val) != 2) {
+                        util::fail('参数不至2个,非法查询方式:' . json_encode($condition));
+                    }
+                    if ($val[0] == 'in') {
+                        $param = ['in', $key, $val[1]];
+                    } elseif ($val[0] == 'between') {
+                        $param = ['between', $key, $val[1][0], $val[1][1]];
+                    } elseif ($val[0] == 'like') {
+                        $param = ['like', $key, $val[1]];
+                    } else {
+                        util::fail('非法查询方式:' . json_encode($condition));
+                    }
+                } else {
+
+                }
+                if ($whereNum == 0) {
+                    $query->where($param);
+                } else {
+                    $query->andWhere($param);
+                }
+                $whereNum++;
+            }
+        }
+        return $query;
+    }
+
+    //是否存在数据 ssh 2019.
+    public function exists($condition)
+    {
+        return $this->conditionQuery($condition)->exists();
+    }
+
+    /**
+     *根据条件查出多条记录
+     */
+    public function getAllByCondition($condition, $order = null, $field, $indexBy = null)
+    {
+        $query = $this->conditionQuery($condition)->select($field);
+        if (isset($indexBy)) {
+            $query->indexBy($indexBy);
+        }
+        if (isset($order)) {
+            $query->orderBy($order);
+        }
+        return $query->asArray()->all();
+    }
+
+    /**
+     * 根据多个主键ID查询多条记录
+     */
+    public function getByIds($ids, $order = null, $indexBy = null)
+    {
+        $data = [];
+        if (empty($ids)) {
+            return $data;
+        }
+        $query = $this->conditionQuery(['id' => ['in', $ids]]);
+        if (isset($indexBy)) {
+            $query->indexBy($indexBy);
+        }
+        if (!empty($order)) {
+            $query->order($order);
+        }
+        $data = $query->asArray()->all();
+        return $data;
+    }
+
+    /**
+     *根据条件查出数量
+     */
+    public function getCount($condition)
+    {
+        return $this->conditionQuery($condition)->count();
+    }
+
+    //分页查询 ssh 2019.9.21
+    public function getList($field, $where, $page, $pageSize, $order = '', $with = '')
+    {
+        $offset = ($page - 1) * $pageSize;
+        $query = $this->conditionQuery($where)->select($field);
+
+        $clone = clone $query;
+        $count = $clone->count();
+        $totalPage = ceil($count / $pageSize);
+        $data['totalNum'] = $count;
+        $data['totalPage'] = $totalPage;//总共页数
+        $data['moreData'] = $totalPage > $page ? 1 : 0;//是否还有更多数据
+
+        if (!empty($order)) {
+            $query->orderBy($order);
+        }
+        if (!empty($with)) {
+            $query->with($with);
+        }
+        $list = $query->offset($offset)->limit($pageSize)->asArray()->all();
+        $data['list'] = $list;//数据
+        return $data;
+    }
+
+    //查询全部 ssh 2019.11.27
+    public function getAllList($field, $where, $order = '', $with = '')
+    {
+        $query = $this->conditionQuery($where)->select($field);
+        if (!empty($order)) {
+            $query->orderBy($order);
+        }
+        if (!empty($with)) {
+            $query->with($with);
+        }
+        $list = $query->asArray()->all();
+        return $list;
+    }
+
+    //查询指定条数 ssh 2019.11.30
+    public function getLimitList($field, $where, $limit, $order = '', $with = '')
+    {
+        $offset = 0;
+        $query = $this->conditionQuery($where)->select($field);
+        if (!empty($order)) {
+            $query->orderBy($order);
+        }
+        if (!empty($with)) {
+            $query->with($with);
+        }
+        $list = $query->offset($offset)->limit($limit)->asArray()->all();
+        return $list;
+    }
+
+    /**
+     * 计数器+1-1
+     * $counters = ['num' => 1] 1数量+1 -1数量-1
+     * $condition = ['id' => $id]
+     */
+    public function counters($counters, $condition, $params = [])
+    {
+        return self::updateAllCounters($counters, $condition, $params);
+    }
 
-	/**
-	 * 支持查询条件包括
-	 * ['name'=>'john','id>'=>3,'id<'=>10,'id'=>['in',[10,11,12]],'age'=>['between',[20,30]],'name'=>['like','Jack']];
-	 */
-	public function conditionQuery($condition = [])
-	{
-		$whereNum = 0;
-		$query = self::find();
-		if (!empty($condition)) {
-			foreach ($condition as $key => $val) {
-				$param = [$key => $val];
-				if (strstr($key, '>')) {
-					//暂不支持大于等于,等于请使用between
-					$pos = strpos($key, '>');
-					$currentKey = substr($key, 0, $pos);
-					$param = ['>', $currentKey, $val];
-				} elseif (strstr($key, '<')) {
-					//暂不支持小于等于,等于请使用between
-					$pos = strpos($key, '<');
-					$currentKey = substr($key, 0, $pos);
-					$param = ['<', $currentKey, $val];
-				} elseif (is_array($val)) {
-					if (count($val) != 2) {
-						util::fail('参数不至2个,非法查询方式:' . json_encode($condition));
-					}
-					if ($val[0] == 'in') {
-						$param = ['in', $key, $val[1]];
-					} elseif ($val[0] == 'between') {
-						$param = ['between', $key, $val[1][0], $val[1][1]];
-					} elseif ($val[0] == 'like') {
-						$param = ['like', $key, $val[1]];
-					} else {
-						util::fail('非法查询方式:' . json_encode($condition));
-					}
-				} else {
-				
-				}
-				if ($whereNum == 0) {
-					$query->where($param);
-				} else {
-					$query->andWhere($param);
-				}
-				$whereNum++;
-			}
-		}
-		return $query;
-	}
-	
-	//是否存在数据 ssh 2019.
-	public function exists($condition)
-	{
-		return $this->conditionQuery($condition)->exists();
-	}
-	
-	/**
-	 *根据条件查出多条记录
-	 */
-	public function getAllByCondition($condition, $order = null, $field, $indexBy = null)
-	{
-		$query = $this->conditionQuery($condition)->select($field);
-		if (isset($indexBy)) {
-			$query->indexBy($indexBy);
-		}
-		if (isset($order)) {
-			$query->orderBy($order);
-		}
-		return $query->asArray()->all();
-	}
-	
-	/**
-	 * 根据多个主键ID查询多条记录
-	 */
-	public function getByIds($ids, $order = null, $indexBy = null)
-	{
-		$data = [];
-		if (empty($ids)) {
-			return $data;
-		}
-		$query = $this->conditionQuery(['id' => ['in', $ids]]);
-		if (isset($indexBy)) {
-			$query->indexBy($indexBy);
-		}
-		if (!empty($order)) {
-			$query->order($order);
-		}
-		$data = $query->asArray()->all();
-		return $data;
-	}
-	
-	/**
-	 *根据条件查出数量
-	 */
-	public function getCount($condition)
-	{
-		return $this->conditionQuery($condition)->count();
-	}
-	
-	//分页查询 ssh 2019.9.21
-	public function getList($field, $where, $page, $pageSize, $order = '', $with = '')
-	{
-		$offset = ($page - 1) * $pageSize;
-		$query = $this->conditionQuery($where)->select($field);
-		
-		$clone = clone $query;
-		$count = $clone->count();
-		$totalPage = ceil($count / $pageSize);
-		
-		$data['totalPage'] = $totalPage;//总共页数
-		$data['moreData'] = $totalPage > $page ? 1 : 0;//是否还有更多数据
-		
-		if (!empty($order)) {
-			$query->orderBy($order);
-		}
-		if (!empty($with)) {
-			$query->with($with);
-		}
-		$list = $query->offset($offset)->limit($pageSize)->asArray()->all();
-		$data['list'] = $list;//数据
-		return $data;
-	}
-	
-	//查询全部 ssh 2019.11.27
-	public function getAllList($field, $where, $order = '', $with = '')
-	{
-		$query = $this->conditionQuery($where)->select($field);
-		if (!empty($order)) {
-			$query->orderBy($order);
-		}
-		if (!empty($with)) {
-			$query->with($with);
-		}
-		$list = $query->asArray()->all();
-		return $list;
-	}
-	
-	//查询指定条数 ssh 2019.11.30
-	public function getLimitList($field, $where, $limit, $order = '', $with = '')
-	{
-		$offset = 0;
-		$query = $this->conditionQuery($where)->select($field);
-		if (!empty($order)) {
-			$query->orderBy($order);
-		}
-		if (!empty($with)) {
-			$query->with($with);
-		}
-		$list = $query->offset($offset)->limit($limit)->asArray()->all();
-		return $list;
-	}
-	
-	/**
-	 * 计数器+1-1
-	 * $counters = ['num' => 1] 1数量+1 -1数量-1
-	 * $condition = ['id' => $id]
-	 */
-	public function counters($counters, $condition, $params = [])
-	{
-		return self::updateAllCounters($counters, $condition, $params);
-	}
-	
-	// 获取图书的作者
-	public function getAuthor()
-	{
-		//同样第一个参数指定关联的子表模型类名
-		return $this->hasOne(Author::className(), ['id' => 'author_id']);
-	}
+    // 获取图书的作者
+    public function getAuthor()
+    {
+        //同样第一个参数指定关联的子表模型类名
+        return $this->hasOne(Author::className(), ['id' => 'author_id']);
+    }
 }