来自2个字段的Yii2搜索表单不起作用

时间:2016-07-11 13:37:02

标签: yii2

我试图从2个字段中搜索表单。但我不能。请帮帮我 image from siteController and site/index

  来自siteController的

代码

        public function actionDriver(){

        $driver = new Driver();

        $journey1 = Driver::findOne(['from' => $this -> from]);
        $journey2 = Driver::findOne(['to' => $this -> to]);

        if($journey1 -> id == $journey2 -> id){

            $driver = $journey1;
            return $this->render('site/index',['driver'=>$driver,
            ]);
        }
    }
  

来自网站/索引的代码

<?php
    use yii\widgets\ActiveForm;
    use yii\helpers\Html;
    ?>
    <?php $form = ActiveForm::begin([
                'action' => ['index'],
                'method' => 'get',
            ]); ?>

            <?= $form->field($driver, 'from') ?>

            <?= $form->field($driver, 'to') ?>

            <div class="form-group">
                <?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
            </div>

            <?php ActiveForm::end(); ?>

1 个答案:

答案 0 :(得分:0)

findOne如果用于pk,你应该使用find - &gt;在哪里检查你是否加载了某些东西

  public function actionDriver(){

    $driver = new Driver();

    if ( $driver->load(Yii::$app->request->post())) {

        $journey1 = Driver::find()->where(['from' => $driver->from])->one();
        $journey2 = Driver::find()->where(['to' => $driver-> to])->one();

        if($journey1 -> id == $journey2 -> id){

           $driver = $journey1;
        }
    } 
    return $this->render('site/index',['driver'=>$driver,]);
  }