/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Cmsposition::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'pageid' => $this->pageid, 'status' => $this->status]); $query->andFilterWhere(['like', 'name', $this->name]); return $dataProvider; }
/** * Finds the Cmsposition model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Cmsposition the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Cmsposition::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Updates an existing Cms model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); $rootPath = "uploads/"; if ($model->load(Yii::$app->request->post())) { $image = UploadedFile::getInstance($model, 'imgurl'); if (empty($image)) { $post = Yii::$app->request->post(); $hidden_imgurl = $post['hidden_imgurl']; if ($hidden_imgurl == '') { $model->addError('imgurl', '请上传图片'); } $model->imgurl = $hidden_imgurl; } else { $ext = $image->getExtension(); $randName = time() . rand(1000, 9999) . "." . $ext; $image->saveAs($rootPath . $randName); $model->imgurl = $rootPath . $randName; } if (strtotime($model->enddate) <= strtotime($model->begindate)) { $model->addError('begindate', '开始时间不能大于结束时间'); } else { $sql = " select id from cms where id<>" . $model->id . " and pageid=" . $model->pageid . " and cmspositionid=" . $model->cmspositionid . " and `sort`=" . $model->sort . " and ((unix_timestamp(begindate) <= unix_timestamp('" . $model->begindate . "') and unix_timestamp(enddate) > unix_timestamp('" . $model->begindate . "')) or (unix_timestamp(begindate) < unix_timestamp('" . $model->enddate . "') and unix_timestamp(enddate) > unix_timestamp('" . $model->enddate . "')) or (unix_timestamp('" . $model->begindate . "') < unix_timestamp(begindate) and unix_timestamp('" . $model->enddate . "') > unix_timestamp(begindate)) or (unix_timestamp('" . $model->begindate . "') > unix_timestamp(enddate) and unix_timestamp('" . $model->enddate . "') < unix_timestamp(enddate))) "; $row = Cms::findBySql($sql)->all(); if (count($row) > 0) { $model->addError('begindate', '同一位置同一时间区间内只能有一条投放广告'); } } if ($model->hasErrors()) { $pagedata = Page::find()->all(); $cmspoaitiondata = Cmsposition::find()->all(); return $this->render('update', ['model' => $model, 'pagedata' => $pagedata, 'cmspoaitiondata' => $cmspoaitiondata]); } //var_dump($model); //die(); if ($model->save()) { return $this->redirect(['view', 'id' => $model->id]); } } else { $pagedata = Page::find()->all(); $cmspoaitiondata = Cmsposition::find()->all(); return $this->render('update', ['model' => $model, 'pagedata' => $pagedata, 'cmspoaitiondata' => $cmspoaitiondata]); } }
public function getCmsposition() { return $this->hasOne(Cmsposition::className(), ['id' => 'cmspositionid']); }