I want to organize a search without using Gridview and display the search results in an array. There is a table with such fields as id ... from, to, and others. It is necessary to organize a search in 2 fields, so if the entered data from both fields (from and to) of one table is in the database, output them in an array.

Search does not work yet. After clicking on the search page is updated and remains on site / index (without displaying any result) I do not know why please help who can. I really want the search to work

code from siteController.php

public function actionSearch() { $driver = new Driver(); if ($driver->load(Yii::$app->request->post())) { $driver = Driver::find() ->where(['from' => $driver->from]) ->andWhere(['to' => $driver->to]) ->one(); return $this->render('search', ['driver' => $driver]); } else { throw new NotFoundHttpException('Input data not found' ); } } 

code from models / Driver.php

 <?php namespace app\models; use yii\base\Model; use yii\data\ActiveDataProvider; use Yii; /** * This is the model class for table "driver". * * @property string $id * @property string $from * @property string $to * @property string $data * @property string $about * @property string $car */ class Driver extends \yii\db\ActiveRecord{ /** * @inheritdoc */ public static function tableName() { return 'driver'; } /** * @inheritdoc */ public function rules() { return [ // [['from', 'to', 'data', 'about', 'car'], 'required'], [['about'], 'string'], [['from', 'to', 'data', 'car'], 'string', 'max' => 255], ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'id' => 'ID', 'from' => 'From', 'to' => 'To', 'data' => 'Data', 'about' => 'About', 'car' => 'Car', ]; } } 

code from site / index.php

 <?php use yii\widgets\ActiveForm; use yii\helpers\Html; use app\models\Driver; /* @var $driver app\models\Driver */ ?> <?php $driver = new Driver; ?> <div class="row"> <div> <?php $form = ActiveForm::begin(); ?> <div class="row"> <div class="col-xs-5"> <?= $form->field($driver, 'from')->label('От')->textInput(['class' => 'input form-control']) ?> </div> <div class="col-xs-5"> <?= $form->field($driver, 'to')->label('До')->textInput(['class' => 'input form-control']) ?> </div> <div class="col-xs-2" align="left" style="margin-top: 30px"> <input type="image" src="<?= \Yii::getAlias('@web/images/button_search.png')?>" class="icon_button" alt="Поиск" > </div> </div> <?php ActiveForm::end(); ?> </div> </div> 

code from site / search.php

  <?php use yii\widgets\LinkPager; $this->title = "Поиск"; $this->registerMetaTag([ 'name' => 'description', 'content' => 'driver', ]); $this->registerMetaTag([ 'name' => 'keywords', 'content' => 'driver', ]) ?> <?php ?> <?php if (!$driver) { ?> <p>Ничего не найдено</p> <?php } else { ?> <?php foreach ($driver as $one){ $from = $one -> from; $to = $one -> to; $data = $one -> data; ?> <div class="one"> <table> <tr> <td> <p><?=$from?></p> </td> <td class="right"> </td> <td class="center"> <p><?=$to?></p> </td> </tr> </table> <div class=""> <?=$data?> <br> <div class="clear"></div> </div> </div> <?php }?> <?php } ?> 

and site / found_drivers.php it is now empty

    1 answer 1

    You incorrectly make requests and incorrectly use ActiveRecord .

    Your query looks like this:

     SELECT * FROM driver WHERE from = "<from>" AND to = "<to>"; 

    Forming it in ActiveRecord requires:

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

    As a result, your method in SiteController will look like this:

     public function actionDriver() { $driver = new Driver(); if ($driver->load(Yii::$app->request->post())) { $driver = Driver::find() ->where(['from' => $driver->from]) ->andWhere(['to' => $driver->to]) ->one(); return $this->render('driver', ['driver' => $driver]); } } 

    I also advise you to re-read the documentation for Yii2

    • Thanks I will take into account but nothing is displayed just a blank sheet in the browser. What could be wrong? - Ildar Nasurlaev pm
    • @IldarNasurlaev You can always see what the problem is in the logs that are in the app / runtime / logs directory. I also advise you to install and configure Xdebug. And finally, check in the file app / config / main.php if you have a debug connected in the 'bootstrap' and 'modules' sections - NPreston
    • Made an update there are some advances but the search still doesn’t work - Ildar Nasurlaev pm
    • @IldarNasurlaev You can see which query is created by the AR with the following command: Driver :: find () -> where (['from' => $ driver-> from]) -> andWhere (['to' => $ driver-> to]) ->) -> createCommand () -> getSql (); - NPreston
    • did update changed site / search.php added variables to it <?php foreach ($driver as $one){ $from = $one -> from; $to = $one -> to; $data = $one -> data; ?> <?php return $this -> render('found_drivers', [ 'from' => $from, 'to' => $to, 'data' => $data ]); ?> <?php }?> <?php foreach ($driver as $one){ $from = $one -> from; $to = $one -> to; $data = $one -> data; ?> <?php return $this -> render('found_drivers', [ 'from' => $from, 'to' => $to, 'data' => $data ]); ?> <?php }?> <?php foreach ($driver as $one){ $from = $one -> from; $to = $one -> to; $data = $one -> data; ?> <?php return $this -> render('found_drivers', [ 'from' => $from, 'to' => $to, 'data' => $data ]); ?> <?php }?> - Ildar Nasurlaev pm