$model = new SearchFile();// форма
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$search = $model->search_file;
$query = File::find()->where(['like', 'name_file', $search])->andWhere(['like', 'type', $search]);
$countQuery = clone $query;
$pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => 10]);
$file = $query->offset($pages->offset)
->limit($pages->limit)
->all();
// этот код выводит пустой массив
} else {
// этот код работаєт
$query = File::find();
$countQuery = clone $query;
$pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => 10]);
$file = $query->offset($pages->offset)
->limit($pages->limit)
->all();
}
public $search_file;
public function rules()
{
return [
[['search_file'], 'trim'],
['search_file', 'string', 'max' => 191]
];
}
/**
* {@inheritdoc}
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = File::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 10,
],
'sort' => [
'defaultOrder' => [
'created' => SORT_DESC,
]
],
]);
$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(['like', 'name_file', $this->search_file]);
// ->andFilterWhere(['like', 'type', $this->search_file]); того поля
return $dataProvider;
}
$model = new File();
if ($model->load(Yii::$app->request->post())) {
$model->file = UploadedFile::getInstance($model, 'file');
if ($model->validate()) {
$name = $model->file->baseName;
$dub = File::find()->where(['like', 'name_file', $name])->one();
if ($dub->name_file != $name){
$type = $model->file->extension;
$size = $model->file->size;
$model->name_file = $name;
$model->type = $type;
$model->size = $size;
$model->save();
$model->file->saveAs('uploads/download/' . $name . '.' . $type);
return $this->redirect(['view', 'id' => $model->id]);
}else{
Yii::$app->session->setFlash('errorSaveFile');
}
}
}
return $this->render('create', [
'model' => $model,
]);
->where(['like', 'name_file', $search])->andWhere(['like', 'type', $search]);