/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Ad::find()->alive(); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => [], 'sort' => ['defaultOrder' => ['order' => SORT_ASC]]]); $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, 'ad_type_id' => $this->ad_type_id, 'order' => $this->order, 'status' => $this->status, 'updated_at' => $this->updated_at, 'created_at' => $this->created_at]); $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'mode', $this->mode])->andFilterWhere(['like', 'pic_url', $this->pic_url])->andFilterWhere(['like', 'text', $this->text])->andFilterWhere(['like', 'link_url', $this->link_url]); return $dataProvider; }
/** * 首页 * @return string */ public function actionIndex() { $columns = Column::find()->where(['type' => Column::CMS_TYPE_LIST])->active()->orderBy('order ASC')->all(); //关于我们单页 $about = Column::find()->where(['short_code' => 'about-brief'])->active()->one(); //首页主广告 $mainAdType = AdType::find()->where(['short_code' => 'home_main'])->active()->one(); //in查询 $mainAds = $mainAdType->getAd()->active()->orderBy('order ASC')->all(); //底部广告 $adBottom = Ad::find()->where(['short_code' => 'home_bottom'])->active()->one(); //搬家现场,推荐 $scenes = Img::find()->select(Img::tableName() . '.*')->where(['like', Img::tableName() . '.flag', 'c'])->joinWith(['column' => function ($query) { $query->where([Column::tableName() . '.short_code' => 'xianchang']); }])->active()->all(); //搬家现场推荐 return $this->render('index', ['columns' => $columns, 'about' => $about, 'mainAdType' => $mainAdType, 'mainAds' => $mainAds, 'adBottom' => $adBottom, 'scenes' => $scenes]); }
/** * (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 (Ad::find()->where(['ad_type_id' => $this->id])->alive()->exists()) { Yii::$app->getSession()->setFlash('warning', Yii::t('cms', 'Under this category contains content, cannot be deleted!')); return false; } } } return true; } else { return false; } }