G
G
Ghost26922018-05-04 18:59:42
Yii
Ghost2692, 2018-05-04 18:59:42

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();
        }

There is data with forms but no data with base data. There is no error and does not show.
What could be the problem?
ps
Search 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;
    }

File upload
$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

[[+comments_count]] answer(s)
M
Maxim Timofeev, 2018-05-07
@webinar

->where(['like', 'name_file', $search])->andWhere(['like', 'type', $search]);

Do you understand that both name_file and type must contain $search? In my opinion this is very strange. If this or that must match, then "or", not "and".
In general, it is difficult for me to imagine that the type of something would be stored as a string. And then there is the type, which contains partially identical sequences with name_file.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question