yii 2.0框架试图显示搜索结果

时间:2015-03-23 15:29:18

标签: php yii2

我有一个名为case_no的字段的提交表单,当我输入case_no时,它应该使用与case_no匹配的特定信息填充表格。

控制器:

public function actionSearch()
{
    $model = new LegalCase();

    $category = new Category();

    /*$categories = Category::findAll();*/

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

        Yii::$app->session->setFlash('searchFormSubmitted');




        $case = $model->findByCaseNo($case_no);
        return $this->render('search', [
            'model' => $model,
            'dataProvider' => $model->findAll(),
            'results' => $case
        ]);

        return $this->refresh();
    } else {
        return $this->render('search', [
            'model' => $model,
        ]);
    }
}

型号:

 class LegalCase extends Model
{
public $id;
public $case_no;
public $name;
public $judgement_date;
public $year;
public $neutral_citation;

private static $cases = [
    '100' => [
        'id' => '1',
        'case_no' => '3',
        'name' => 'first case'  
    ],
    '101' => [
        'id' => '1',
        'case_no' => '125',
        'name' => 'second case'
    ],
];

/**
 * @inheritdoc
 */
public static function findAll()
{

    //$query = new Query();
    $provider = new ArrayDataProvider([
        'allModels' => self::$cases,
        'sort' => [
            'attributes' => ['id', 'case_no', 'name'],
        ],
        'pagination' => [
            'pageSize' => 10,
        ],
    ]);

    return $provider->getModels();
}

/**
 * @inheritdoc
 */
public static function findIdentity($id)
{
    return isset(self::$cases[$id]) ? new static(self::$cases[$id]) : null;
}

/**
 * Finds case by name
 *
 * @param  string      $name
 * @return static|null
 */



/**
 * Finds case by number
 *
 * @param  string      $case_no
 * @return static|null
 */
public static function findByCaseNo($case_no)
{
    foreach (self::$cases as $case) {
        if (strcasecmp($case['case_no'], $case_no) === 0) {
            return new static($case);
        }
    }

    return null;
}

查看:

 <?php
 use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\widgets\ListView;

use app\models\LegalCase;

use backend\models\Standard;


/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model app\models\LegalCase */

$this->title = 'Search';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-login">
<h1><?= Html::encode($this->title) ?></h1>

<p>Please fill out the following fields to login:</p>

<?php $form = ActiveForm::begin([
    'id' => 'search-form',
    'options' => ['class' => 'form-horizontal'],
    'fieldConfig' => [
        'template' => "{label}\n<div class=\"col-lg-3\">{input}   </div>\n<div class=\"col-lg-8\">{error}</div>",
        'labelOptions' => ['class' => 'col-lg-1 control-label'],
    ],
]); ?>

<?= $form->field($model, 'case_no') ?>

<?= $form->field($model, 'name') ?>

<?php /*= Html::activeDropDownList($model, 's_id',
  ArrayHelper::map(Standard::find()->all(), 's_id', 'name'))*/  ?>

<?php


?>





<div class="form-group">
    <div class="col-lg-offset-1 col-lg-11">
        <?= Html::submitButton('Search', ['class' => 'btn btn-primary', 'name' => 'search-button']) ?>
    </div>
</div>

<?php if (Yii::$app->session->hasFlash('searchFormSubmitted')) { ?>

    <table class="table table-striped">
        <tr>
            <th>ID</th>
            <th>case_no</th>
            <th>name</th>
        </tr>
    </table>

    <?php //$posts = $results->getModels();
    foreach($results as $case){
        echo '<tr>';
        //print_r($case);
        echo $case;
        //echo $case->id;
        /*echo '<td>' . $post['id'] . '</td>';
        echo '<td>' . $post['case_no'] . '</td>';
        echo '<td>' . $post['name'] . '</td>';*/
        echo '</tr>';
    } ?>

知道为什么这不起作用吗?

2 个答案:

答案 0 :(得分:1)

使用$_POST['LegalCase']['case_no']代替$case_no

编辑:注意:我使用的是Yii的旧版本,所以事情可能已经发生了变化。

答案 1 :(得分:1)

你需要这样做:

$case = $model->findByCaseNo($model->case_no);

但是在表单提交后,您的属性$ case_no将为空。发生这种情况是因为属性不安全。函数load()仅设置安全模型属性。如果要在提交后加载模型属性,则需要向LegalCase模型添加验证。例如:

public function rules()
{
     return [
         ['case_no', 'trim'],
     ];
}