Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alanwillms/7684f6536577158fe4ee to your computer and use it in GitHub Desktop.
Save alanwillms/7684f6536577158fe4ee to your computer and use it in GitHub Desktop.
Classes Query e Search de um modelo ActiveRecord
<?php
namespace app\models\query;
use app\components\ActiveQuery;
class PublicationQuery extends ActiveQuery
{
/**
* @return PublicationQuery
*/
public function featured()
{
$this->andWhere('featured = TRUE');
return $this;
}
/**
* @return PublicationQuery
*/
public function unblocked()
{
$this->andWhere('blocked = FALSE');
return $this;
}
/**
* @return PublicationQuery
*/
public function published()
{
$this->andWhere('published = TRUE');
return $this;
}
}
/////////////
namespace app\models\search;
use app\components\SearchModel;
use Yii;
use yii\data\Sort;
class PublicationSearch extends SearchModel
{
public $title;
public $original_title;
public $user_id;
public $published;
public $category_id;
public $author_id;
public $unblocked_only = false;
public $published_only = false;
/**
* @return array
*/
public function rules()
{
return [
[['author_id', 'category_id', 'user_id'], 'integer'],
[['!unblocked_only', '!published_only', 'published'], 'boolean'],
[['title', 'original_title'], 'safe'],
];
}
/**
* @param array $params
* @return \yii\data\ActiveDataProvider
*/
public function search($params = [])
{
$dataProvider = parent::search($params);
$dataProvider->pagination->pageSize = 30;
$dataProvider->pagination->pageParam = 'page';
return $dataProvider;
}
/**
* @param \yii\db\ActiveQuery $query
*/
public function searchConditions($query)
{
if ($this->category_id) {
$query->join('JOIN', 'categories_publications cp', 'cp.publication_id = publications.id');
$query->andWhere('cp.category_id = :category', [':category' => $this->category_id]);
}
if ($this->author_id) {
$query->join('JOIN', 'authors_pseudonyms_publications app', 'app.publication_id = publications.id');
$query->andWhere('app.author_id = :author', [':author' => $this->author_id]);
}
if ($this->unblocked_only) {
$query->andWhere('publications.blocked = FALSE');
}
if ($this->published_only) {
$query->andWhere('publications.published = TRUE');
}
$this->addCondition($query, 'title', true);
$this->addCondition($query, 'original_title', true);
$this->addCondition($query, 'user_id');
if ($this->published !== null && $this->published !== '') {
$condition = 'publications.published = ' . ($this->published ? 'TRUE' : 'FALSE');
$query->andWhere($condition);
}
$query->distinct();
}
/**
* @inheritdoc
*/
protected function getSort()
{
return new Sort(
[
'defaultOrder' => [
'created_at' => SORT_DESC,
],
'attributes' => [
'created_at' => [
'default' => SORT_DESC,
'label' => Yii::t('discussions', 'sort_by_date_label'),
],
'hits' => [
'default' => SORT_DESC,
'label' => Yii::t('publications', 'sort_by_hits_label'),
],
'title' => [
'default' => SORT_ASC,
'label' => Yii::t('publications', 'sort_by_title_label'),
],
],
]
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment