我收到错误undefined variable model
。我在视图和控制器中有以下代码。我的目的是搜索结果并在同一页面中显示。我尝试过以下的东西,但它不起作用。
同样,我这是传统的yii方法,或者我必须使用模型SearchEmployee()中的search()函数。?
public function actionSearch()
{
$model = new SearchEmployee();
/*Getting Data From Search Form For Processing */
if (isset($_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",
));
$this->render('search',$model);
}
/*Getting Data From Search Form For Processing */
$this->render('search', array('model' => $model));
}
在视图部分:search.php //显示和搜索所需的结果
<div class="row">
<?php echo $form->labelEx($model,'Category'); ?>
<?php echo $form->dropDownList($model,'category_id',$list,array('empty' =>'(Select a Category')); ?>
<?php echo $form->error($model,'category'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'Experience'); ?>
<?php echo $form->textField($model,'experience'); ?>
<?php echo $form->error($model,'experience'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Search'); ?>
</div>
<div class="view">
<h1>Results </h1>
<div class="view" id="id">
<h1> Records Display </h1>
<h4>Name: <?php echo $form->labelEx($model,'name'); ?></h4>
<h4>Skills: <?php echo $form->labelEx($model,'experience'); ?></h4>
<h4>Experience: <?php echo $form->labelEx($model,'skills'); ?></h4>
<h5>   ;<?php echo $form->labelEx('VIew Details'); ?></h5>
</div>
</div>
在视图部分,我使用相同表单中的搜索和查看结果选项。点击搜索按钮后出现错误。这是正确的方法
答案 0 :(得分:1)
$model = SearchEmployee::model()->find(array(
'select' => array('*'), "condition" => "category_id=$category AND
key_skills like '%$skills%' AND experience=$experience",
));
这里有错误。你不能以这种方式使用find。
public function find($condition='',$params=array())
$model = SearchEmployee::model()->find("category_id=$category AND
key_skills like '%$skills%' AND experience=$experience");
但更好
$model = SearchEmployee::model()->find("category_id=:category AND key_skills like :skill AND experience=:experience", array(
'category'=>$category,
'skill'=>'%'.$skills.'%',
'experience'=>$experience
));
答案 1 :(得分:0)
删除此行:
$this->render('search',$model);
首先它无效,因为它应该是:
$this->render('search', array('model' => $model));
但也没必要,因为你的代码已经低了它。