예제 #1
0
 public function actionIndex()
 {
     //        $category = Category::model()->findByPk(5);
     //        $childs = $category->descendants()->findAll();
     //        foreach ($childs as $c)
     //            $ids[] = $c->id;
     //        $ids_string = implode(',', $ids);
     //        $criteria = new CDbCriteria(array(
     //            'condition' => 'status=' . Post::STATUS_PUBLISHED .' AND  category_id in ('.$ids_string.')',
     //            'order' => 'create_time DESC',
     //            'with' => 'commentCount',
     //        ));
     //        if (isset($_GET['tag']))
     //            $criteria->addSearchCondition('tags', $_GET['tag']);
     $search = \Yii::$app->request->get('tag');
     $catgegories = Tree::findOne(['name' => '单页分类']);
     $children = $catgegories->children()->all();
     foreach ($children as $c) {
         $ids[] = $c->id;
     }
     $ids_string = implode(',', $ids);
     $dataProvider = new ActiveDataProvider(['query' => !empty($search) ? Post::find()->where('category_id not in (' . $ids_string . ')')->orderBy('id desc')->andWhere(['like', 'tags', $search]) : Post::find()->where('category_id not in (' . $ids_string . ')')->orderBy('id desc'), 'pagination' => ['pageSize' => 20]]);
     //        $dataProvider = new ActiveDataProvider('Post', array(
     //            'pagination' => array(
     //                'pageSize' => Yii::app()->params['postsPerPage'],
     //            ),
     //            'criteria' => $criteria,
     //        ));
     return $this->render('index', array('dataProvider' => $dataProvider));
 }
예제 #2
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Post::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'category_id' => $this->category_id, 'user_id' => $this->user_id, 'language_id' => $this->language_id, 'star_id' => $this->star_id, 'cluster_id' => $this->cluster_id, 'station_id' => $this->station_id, 'status' => $this->status, 'views' => $this->views, 'allow_comment' => $this->allow_comment, 'create_time' => $this->create_time, 'update_time' => $this->update_time]);
     $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'url', $this->url])->andFilterWhere(['like', 'source', $this->source])->andFilterWhere(['like', 'summary', $this->summary])->andFilterWhere(['like', 'content', $this->content])->andFilterWhere(['like', 'tags', $this->tags]);
     $query->orderBy('id desc');
     return $dataProvider;
 }
예제 #3
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getPost()
 {
     return $this->hasOne(Post::className(), ['id' => 'post_id']);
 }
예제 #4
0
 /**
  * Finds the Post model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Post the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Post::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }