Answer the question
In order to leave comments, you need to log in
Why is controller search not working, Yii2?
$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,
]);
Answer the question
In order to leave comments, you need to log in
->where(['like', 'name_file', $search])->andWhere(['like', 'type', $search]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question