$query->andFilterWhere([
'or',
['like', 'name_file', $this->search_file],
['like', 'type', $this->search_file]
]);
<?php
namespace app\modules\admin\models;
use Yii;
/**
* This is the model class for table "file".
*
* @property int $id
* @property string $name_file
* @property string $type
* @property int $size
*/
class File extends \yii\db\ActiveRecord
{
public $file;
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'file';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['file'], 'file', 'skipOnEmpty' => false],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name_file' => yii::t('app', 'Назва файла'),
'file' => yii::t('app', 'Файл'),
'type' => yii::t('app', 'Тип'),
'size' => yii::t('app', 'Розмір'),
'created' => yii::t('app', 'Створено'),
];
}
}
<?php $form = ActiveForm::begin([
'options' => ['class' => 'form-inline float-right'],
'action' => ['download'],
'method' => 'get',
]);
?>
<div class="form-group mx-sm-3 mb-2">
<?= $form->field($model, 'search_file')->textInput(['class' => 'form-control form-control-sm', 'placeholder' => 'Знайти файл'])->label(false) ?>
</div>
<?= Html::submitButton('Знайти', ['class' => 'btn btn-sm btn-primary mb-2']) ?>
<?php ActiveForm::end(); ?>
$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,
]);
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;
}
а public $file; то до загрузки файлів, до пошуку нічого спільного немає