public function actionCategory($slug) { $sql = 'SELECT id, title, keywords, description FROM category WHERE slug=:slug'; $category = \app\modules\category\models\Category::findBySql($sql, [':slug' => $slug])->one(); $dataProvider = new ActiveDataProvider(['query' => Post::find()->where(['cat_id' => $category->id, 'status' => 1]), 'pagination' => ['pageSize' => 10]]); return $this->render('category', ['dataProvider' => $dataProvider, 'category' => $category]); }
/** * 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 return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'category_id' => $this->category_id, 'created_by' => $this->created_by, 'created_date' => $this->created_date, 'publish_date' => $this->publish_date, 'updated_date' => $this->updated_date, 'views' => $this->views, 'active' => $this->active]); $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'alias', $this->alias])->andFilterWhere(['like', 'desciption', $this->desciption])->andFilterWhere(['like', 'content', $this->content])->andFilterWhere(['like', 'meta_title', $this->meta_title])->andFilterWhere(['like', 'meta_desciption', $this->meta_desciption]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Post::find(); // add conditions that should always apply here $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['created_at' => '']], 'pagination' => ['pageSize' => 10]]); $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; } // grid filtering conditions $query->andFilterWhere(['id' => $this->id, 'status' => $this->status, 'main' => $this->main, 'cat_id' => $this->cat_id, 'FROM_UNIXTIME(created_at, "%d.%m.%Y")' => $this->created_at]); $query->andFilterWhere(['like', 'username', $this->username])->andFilterWhere(['like', 'title', $this->title]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * @param integer $pageSize The number of results to be displayed per page. * @param boolean $published Whether or not posts have to be published. * * @return ActiveDataProvider */ public function search($params, $pageSize = 3, $published = false) { $query = Post::find(); // this means that editor is trying to see posts // we will allow him to see published ones and drafts made by him if ($published === true) { $query->where(['status' => Post::STATUS_PUBLISHED]); $query->orWhere(['user_id' => Yii::$app->user->id]); } $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['id' => SORT_DESC]], 'pagination' => ['pageSize' => $pageSize]]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'user_id' => $this->user_id, 'status' => $this->status, 'type' => $this->type]); $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'content', $this->content]); return $dataProvider; }
public function actionIndex() { $posts = Post::find()->all(); $cache = \Yii::$app->getCache(); $cache->flush(); foreach ($posts as $item) { // var_dump($item); $post = clone $item; $post->alias = \yii\helpers\Inflector::slug($post->title); $post->created_date = time(); $post->publish_date = time(); try { $post->save(); } catch (Exception $ex) { var_dump($ex); } } return $this->render('index', ['posts' => $posts]); }
/** * Lists all Post models. * @return mixed */ public function actionIndex() { $searchModel = new PostSearch(); $dataProvider = $searchModel->search(Yii::$app->request->post()); //setlocale(LC_ALL, 'en_US.UTF-8'); setlocale(LC_CTYPE, 'ru_RU.UTF-8'); //Lucene\Lucene::setDefaultSearchField('contents'); Lucene\Search\QueryParser::setDefaultEncoding('UTF-8'); Lucene\Analysis\Analyzer\Analyzer::setDefault(new Lucene\Analysis\Analyzer\Common\Utf8\CaseInsensitive()); Lucene\Lucene::setResultSetLimit(10); // create blog posts index located in /data/posts_index ,make sure the folder is writable $index = Lucene\Lucene::create('data/posts_index'); $posts = Post::find()->all(); //var_dump($posts);die(); // iterate through posts and build the index foreach ($posts as $p) { $doc = new Lucene\Document(); $doc->addField(Lucene\Document\Field::UnIndexed('entry_id', $p->id)); $doc->addField(Lucene\Document\Field::Keyword('title', $p->title)); $doc->addField(Lucene\Document\Field::text('contents', $p->content)); $index->addDocument($doc); } // commit the index $index->commit(); //Lucene\Analysis\Analyzer\Analyzer::setDefault(new Lucene\Analysis\Analyzer\Common\Utf8\CaseInsensitive()); // explode the search query to individual words $words = explode(' ', urldecode(Yii::$app->getRequest()->getQueryParam('q'))); // start a search query and add a term for each word to it $query = new Lucene\Search\Query\MultiTerm(); foreach ($words as $w) { $query->addTerm(new Lucene\Index\Term($w)); } // open and query the index $index = Lucene\Lucene::open('data/posts_index'); $results = $index->find($query); // the search results //var_dump($results); return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'search' => $results, 'query' => $query]); }