Skip to content

Commit

Permalink
refactor: 优化查询支持多库
Browse files Browse the repository at this point in the history
  • Loading branch information
jinxing committed Oct 20, 2021
1 parent 1f4c81d commit e5eeb26
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
9 changes: 4 additions & 5 deletions src/controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected function getQuery($where)
{
/* @var $model \yii\db\ActiveRecord */
$model = $this->modelClass;
return (new Query())->from($model::tableName())->where($where);
return $model::find()->where($where)->asArray();
}

/**
Expand Down Expand Up @@ -129,9 +129,9 @@ public function actionSearch()
$query = $this->getQuery(ArrayHelper::getValue($search, 'where', []));

// 查询数据条数
if ($total = $query->count('*', $this->modelClass::getDb())) {
if ($total = $query->count()) {
$orderBy = ArrayHelper::getValue($search, 'orderBy') ?: [$this->sort => SORT_DESC];
if ($array = $query->offset($search['offset'])->limit($search['limit'])->orderBy($orderBy)->all($this->modelClass::getDb())) {
if ($array = $query->offset($search['offset'])->limit($search['limit'])->orderBy($orderBy)->all()) {
$this->afterSearch($array);
}
} else {
Expand Down Expand Up @@ -331,7 +331,7 @@ public function actionUpload()
{
// 接收参数
$request = Yii::$app->request;
$strField = $request->get('sField'); // 上传文件表单名称
$strField = $request->get('sField'); // 上传文件表单名称
if (empty($strField)) {
return $this->error(201);
}
Expand Down Expand Up @@ -447,7 +447,6 @@ public function actionExport()
$strTitle,
$arrFields,
$this->getQuery($conditions)->orderBy([$this->sort => SORT_DESC]),
$this->modelClass,
$this->getExportHandleParams()
);
}
Expand Down
10 changes: 3 additions & 7 deletions src/helpers/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ public static function strToUpperWords($strName, $and = '_')
* @throws \PHPExcel_Writer_Exception
* @throws \yii\base\ExitException
*/
public static function excel($title, $columns, $query, $model, $handleParams = [], $function = null)
public static function excel($title, $columns, $query, $handleParams = [], $function = null)
{
$intCount = $query->count('*', $model::getDb());
$intCount = $query->count();
// 判断数据是否存在
if ($intCount <= 0) {
return;
Expand Down Expand Up @@ -226,11 +226,7 @@ public static function excel($title, $columns, $query, $model, $handleParams = [

// 写入数据信息
$intNum = 2;
$order = [];
foreach ($model::primaryKey() as $val) {
$order[$val] = SORT_DESC;
}
foreach ($query->orderBy($order)->batch(1000, $model::getDb()) as $array) {
foreach ($query->batch(1000) as $array) {

// 函数处理,允许修改数据
if ($function instanceof Closure) {
Expand Down

0 comments on commit e5eeb26

Please sign in to comment.