在yii中搜索操作

时间:2014-04-17 06:39:39

标签: php yii

我正在尝试在我的项目中实现搜索操作,即作业门户。我正在尝试在同一页面上显示结果,不知道该怎么做。我做了以下事情。

控制器

$model = new SearchEmployee();

/*Getting Data From Search Form For Processing */
if (isset($_POST['SearchEmployee'])) {

    $model->attributes = $_POST['SearchEmployee'];

    $category   =  $_POST['SearchEmployee']['category_id'];
    $skills     =  $_POST['SearchEmployee']['skills'];
    $experience =  $_POST['SearchEmployee']['experience'];

    $model = SearchEmployee::model()->find(array(
            'select' => array('*'), "condition" => "category_id=$category AND key_skills like'%$skills%'AND experience=$experience",

    ));

    if($model==null)
    {
        Yii::app()->user->setFlash('success', "No Results");
        $this->render('search');
    }
    else
    {
        $this->render('search', array('model' => $model));
    }

}
/*Getting Data From Search Form For Processing */
$this->render('search', array('model' => $model));

查看:简要代码

<?php
/* @var $this SiteController */
/* @var $model LoginForm */
/* @var $form CActiveForm  */

$this->pageTitle=Yii::app()->name . ' - Search Employee';
$this->breadcrumbs=array(
        'Search Employee',
);

$list = CHtml::listData(Category::model()->findAll(),'id','title');

?>
<h1>Search Employees</h1>
<div class="form">
    <?php $form=$this->beginWidget('CActiveForm', array(
            'id'=>'login-form',
            'enableClientValidation'=>true,
            'clientOptions'=>array(
            'validateOnSubmit'=>true,
        ),
    )); ?>

    //Search Form//
    <div class="row">
        <?php echo $form->labelEx($model,'Skills'); ?>
        <?php echo $form->textField($model,'skills'); ?>
        <?php echo $form->error($model,'skills'); ?>
    </div>
    //Search Form// //Result Display//
    <div class="view">

        <h1>Results</h1>

        <div class="view" id="id">

            <h1>Records Display</h1>

            <h4>
                Name:
                <?php echo $form->labelEx($model,'skills Required'); ?>
            </h4>
            <h4>
                Skills:
                <?php echo $form->labelEx($model,'Skills Required'); ?>
            </h4>
            <h4>
                Experience:
                <?php echo $form->labelEx($model,'Skills Required'); ?>
            </h4>
            <h5>
                &nbsp;&nbsp;
                <?php echo CHtml::submitButton('VIew Details'); ?>
            </h5>
        </div>

    </div>

它正在工作,直到模型代码被执行,但是当redirectig回到页面时它显示错误。 &#39;模型未定义&#39;。

1 个答案:

答案 0 :(得分:0)

好的,显然有更多的错误..让我们从头开始尝试:

“未定义模型” - 您得到的原因是,如果$model为空,则使用search参数呈现$model视图。在搜索视图中,您尝试使用$model变量,但当然它不存在。您必须更改视图,以便在未定义时不使用模型。或者在搜索中至少发送一个空模型

$this->render('search',new SearchEmployee);

在设置属性后,您应该正常调用$model->search()。但似乎你自己创建了查询,所以你可以完全跳过这一行:

$model->attributes = $_POST['SearchEmployee'];