You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.6 KiB

7 years ago
<?php
namespace common\modules\blog\forms\search;
use common\modules\blog\entities\BlogComment;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
class BlogCommentSearch extends Model
{
public $id;
public $text;
public $active;
public $post_id;
public function rules(): array
{
return [
[['id', 'post_id'], 'integer'],
[['text'], 'safe'],
[['active'], 'boolean'],
];
}
/**
* @param array $params
* @return ActiveDataProvider
*/
public function search(array $params): ActiveDataProvider
{
$query = BlogComment::find()->with(['post']);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'key' => function (BlogComment $comment) {
return [
'post_id' => $comment->post_id,
'id' => $comment->id,
];
},
'sort' => [
'defaultOrder' => ['id' => SORT_DESC]
]
]);
$this->load($params);
if (!$this->validate()) {
$query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'post_id' => $this->post_id,
]);
$query
->andFilterWhere(['like', 'text', $this->text]);
return $dataProvider;
}
public function activeList(): array
{
return [
1 => Yii::$app->formatter->asBoolean(true),
0 => Yii::$app->formatter->asBoolean(false),
];
}
}