/** * Finds the Link model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Link the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Link::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * 假删除动作(重写) * @param integer $id * @return \yii\web\Response */ public function actionDelete($id) { if (Link::find()->where(['link_type_id' => $id])->alive()->exists()) { Yii::$app->getSession()->setFlash('warning', Yii::t('extend', 'Have links under the link type, cannot be deleted')); } else { $model = $this->findModel($id); $model->deleted = 1; $model->save(false); //更新 } return $this->redirect(['index']); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Link::find()->alive(); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC]]]); $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, 'link_type_id' => $this->link_type_id, 'order' => $this->order, 'status' => $this->status, 'deleted' => $this->deleted, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'description', $this->description])->andFilterWhere(['like', 'pic_url', $this->pic_url])->andFilterWhere(['like', 'link_url', $this->link_url]); return $dataProvider; }
/** * (non-PHPdoc) * @see \yii\db\BaseActiveRecord::beforeSave($insert) */ public function beforeSave($insert) { if (parent::beforeSave($insert)) { if ($insert) { //创建 } else { //更新 //判断是否为假删除操作 if (!empty(Yii::$app->requestedAction->feild) && Yii::$app->requestedAction->feild == 'deleted') { //当前分类下有没有子分类 if (Link::find()->where(['link_type_id' => $this->id])->alive()->exists()) { Yii::$app->getSession()->setFlash('warning', Yii::t('extend', 'Under this category contains content, cannot be deleted!')); return false; } } } return true; } else { return false; } }
if (!empty($this->params['breadcrumbs'])) { $params = ['tag' => 'ul', 'encodeLabels' => false, 'itemTemplate' => "<li>{link}<span class=\"jian\">></span></li>\n", 'options' => ['class' => 'breadcrumb clearfix'], 'homeLink' => ['label' => '首页', 'url' => Yii::$app->homeUrl], 'links' => $this->params['breadcrumbs']]; echo Breadcrumbs::widget($params); } ?> <?php echo $content; ?> </div> <div class="foot-top clearfix mar-t18"> <div class="foot-top-content clearfix"> <div class="n_foot-box"> <?php $links = Link::find()->select(Link::tableName() . '.*')->joinWith(['linkType' => function ($query) { $query->where([LinkType::tableName() . '.short_code' => 'site_links']); }])->active()->all(); //友情链接 ?> <ul class="tit_tab clearfix"> <li class="on">友情链接</li> </ul> <div class="main-link-box clearfix"> <ul class="clearfix"> <?php foreach ($links as $link) { ?> <li> <?php echo Html::a($link->name, [$link->link_url], ['target' => '_blank']);