request; $city = $request->get('city'); $district = $request->get('district'); $keyword = $request->get('keyword'); $type = isset($_GET['type']) ? $_GET['type'] : null; /** * 获取地址类型label */ $typeLables = AddressModel::typeLabels(); /** * 查询过滤处理 */ $get = []; $get['city'] = empty($city) ? "" : $city; $get['district'] = empty($district) ? "" : $district; $get['keyword'] = empty($keyword) ? "" : $keyword; $get['type'] = ($type == "") ? null : $type; $query = AddressModel::find() ->select('a.*, su.province, su.city, su.district') ->from(AddressModel::tableName() . 'a') ->orderBy('a.id DESC') ->leftJoin(AddressProfileModel::tableName() . 'su', "a.id = su.address_id"); if (!empty($city)) { $query->andWhere(['like', 'su.city', $city]); } if (!empty($district)) { $query->andWhere(['like', 'su.district', $district]); } if (!empty($keyword)) { $query->orWhere(['like', 'a.address', $keyword]); $query->orWhere(['like', 'a.title', $keyword]); $query->orWhere(['like', 'a.detail', $keyword]); } if ($get['type'] !== null) { $query->andWhere(['=', 'a.type', $type]); } /** * 分页处理 */ $pageSize = $request->get("pageSize") ? (int)$request->get("pageSize") : 50; $pages = new Pagination(['totalCount' => $query->count(), 'pageSize' => $pageSize]); $query->offset($pages->offset)->limit($pages->limit)->all(); $model = $query->asArray()->all(); return $this->render('index', array( 'address' => $model, 'pages' => $pages, 'typeLables' => $typeLables, 'gets' => $get )); } /** * 地址详情页 */ public function actionUpdate($id) { $model = $this->findModel($id); return $this->render('update', [ 'model' => $model, ]); } /** * 执行更新操作 */ public function actionDoUpdate() { $req = Yii::$app->request; $id = (int)$req->post('id'); $address = $req->post('address'); $title = $req->post('title'); $detail = $req->post('detail'); $longitude = $req->post('longitude'); $latitude = $req->post('latitude'); $model = $this->findModel($id); $model->address = $address; $model->detail = $detail; $model->title = $title; $model->longitude = $longitude; $model->latitude = $latitude; $res = $model->save(); if ($res) { Yii::$app->session->setFlash('success', '保存地址成功'); return $this->renderJson(array("status" => 1, "msg" => "操作成功")); } else { return $this->renderJson(array("status" => 0, "msg" => "操作失败")); } } /** * 根据主键查找模型 */ protected function findModel($id) { $obj = Yii::createObject(AddressModel::className()); if (($model = $obj::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('访问页面不存在'); } } }