modelname及其行为没有名为“getData”的方法或闭包

时间:2016-03-08 09:26:50

标签: php yii

我的控制器在那里

defaultController.php

public function actionBusSearch() {
  $model = new Busdetails;
  if (isset($_POST['origin']) && isset($_POST['destination']) && isset($_POST['departdate']) && isset($_POST['returndate'])) {
      $origin = $_POST['origin'];
      $destination = $_POST['destination'];
      $departdate = $_POST['departdate'];
      $returndate = $_POST['returndate'];
      $query = BusDetails::model()->findAllByAttributes(array(
          'from_place' => $origin,
          'to_place' => $destination,
      ));
      $this->render('searchresult', array('model' => $query)); //here model is variable in we can pass query data into searchresult.php page
  }
}

我的 searchresult.php

<?php
foreach ($model as $demo) {
}
?>

<?php
$this->widget('zii.widgets.grid.CGridView', array(
   'id' => 'search-grid',
    'dataProvider' => $model, //maybe the issue is with this criteria? It is currently 'as-is' after the template generation
    'filter' => $model,
    'columns' => array(
    'travel_name',
    'depart_time',
    'arrive_time',
    ),
));
?>

但无法显示CGridview。什么问题。我是yii的新人。请帮帮我。

1 个答案:

答案 0 :(得分:0)

你错过了Yii的一些概念,例如dataProvidersfilters into CGridView

您需要修改操作:

public function actionBusSearch() {
  $model = new Busdetails;
  if (isset($_POST['origin']) && isset($_POST['destination']) && isset($_POST['departdate']) && isset($_POST['returndate'])) {
      $origin = $_POST['origin'];
      $destination = $_POST['destination'];
      $departdate = $_POST['departdate'];
      $returndate = $_POST['returndate'];
      $model = new BusDetails('search');
      $dataProvider = new CActiveDataProvider('Busdetails', array(
        'criteria'=>array(
            'condition'=>'from_place=:from_place AND to_place=:to_place',
            'params' => array(':from_place' => $origin, ':to_place' => $destination)
        ),
        'pagination'=>array(
            'pageSize'=>20,
        ),
    ));
      $this->render('searchresult', array(
          'model' => $model, 
          'dataProvider' => $dataProvider
      ));
  }
}

您需要修改视图:

<?php
$this->widget('zii.widgets.grid.CGridView', array(
   'id' => 'search-grid',
    'dataProvider' => $dataProvider,
    'filter' => $model,
    'columns' => array(
        'travel_name',
        'depart_time',
        'arrive_time',
    ),
));