Yii2:如果$ model = Model :: find()-> all();,则为未定义变量未在视图中指定

时间:2018-10-29 18:39:33

标签: php yii2

我有一个视图,需要同时显示当前记录表和在表中进行搜索。问题是,尽管URL显示了搜索参数,但是在执行搜索操作后,该表仍显示所有记录。我想这是因为模型($posts = Post::find()->all();)中指定的所有记录。

但是,如果我删除该字符串,则会出现异常“未定义变量:posts”。 怎么解决?

型号:

class Post extends ActiveRecord
{
    public static function tableName()
    {
        return 'post';
    }

    public $upload;

    public function rules()
    {
        return [
            [['desc'], 'required'],
            [['desc'], 'string', 'max' => 255],
            [['upload'], 'file'],
        ];
    }
}

控制器:

public function actionSearch()
    {

        if (Yii::$app->request->get()){
            $search = Yii::$app->request->get('search');
            $query = Post::find()->where(['like', 'desc', $search]);

            return $this->render('search', compact('query', 'search'));
        } else {
            $files = Post::find()->all();

            return $this->render('search', ['files' => $files]);
        }
    }

查看:

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use app\models\Post;

$this->title = 'Search';

$posts = Post::find()->all();
?>

<div class="site-search card border-secondary">
    <div class="card-header"><h3><?= Html::encode($this->title) ?></h3></div>
    <?php $form = ActiveForm::begin([
            'action' => ['search'],
            'method' => 'get',
    ]) ?>
    <div class="body-content card-body">
        <div class="form-group row">
            <label class="col-sm-4 col-form-label">Description</label>
            <div class="col-sm-8">
                <?= Html::textInput('search','', ['class' => 'form-control']) ?>
            </div>
        </div>
        <div class="col-sm-offset-4">
            <?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
        </div>
        <table class="table">
            <thead>
                <tr>
                    <th class="col-md-4">Name</th>
                    <th class="col-md-6">Description</th>
                    <th class="col-md-2">Action</th>
                </tr>
            </thead>
            <tbody>
            <?foreach ($posts as $post):?>
                <tr>
                    <td class="hidden"><?=$post->id?></td>
                    <td><?=$post->file?></td>
                    <td><?=$post->desc?></td>
                    <td><?= Html::a('Download', ['download', 'id'=>$post->id]) ?></td>
                </tr>
            <?endforeach;?>
            </tbody>
        </table>
    </div>
    <?php ActiveForm::end() ?>
</div>

2 个答案:

答案 0 :(得分:2)

您需要将$posts值传递给视图:

public function actionSearch() {
    if (Yii::$app->request->get('search')){
        $search = Yii::$app->request->get('search');
        $posts = Post::find()->where(['like', 'desc', $search])->all();
    } else {
        $posts = Post::find()->all();
    }

    return $this->render('search', ['posts' => $posts]);
}

然后,您可以从视图中删除$posts = Post::find()->all();

答案 1 :(得分:0)

在视图中,您引用$ posts,但在actionSearch中,您使用$ files 在您的操作中,更改$ posts中的$ files

  public function actionSearch()
  {

      if (Yii::$app->request->get()){
          $search = Yii::$app->request->get('search');
          $posts= Post::find()->where(['like', 'desc', $search]);

          return $this->render('search', compact('query', 'search'));
      } else {
          $posts = Post::find()->all();

          return $this->render('search', ['posts' => $posts]);
      }
  }

,然后在视图中删除$ postes

  $this->title = 'Search';

  //$posts = Post::find()->all();
  ?>