2014-04-17 116 views
0

我正在嘗試在我的項目中實現搜索操作,即job portal。我試圖在同一頁上顯示結果,不知道該怎麼做。我做了以下事情。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回到頁面顯示的錯誤。 '模型未定義'。

回答

0

好吧,顯然有更多的錯誤..讓我們從一開始就嘗試:

「模型沒有定義」 - 你,因爲如果$model爲空,則呈現search視圖withou的$model參數。在搜索視圖中,您嘗試使用$model變量,但它當然不存在。你必須改變你的視圖,以便它在未定義的情況下不使用模型。或在搜索

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

發送至少一個空的模型和你應該叫$model->search()通常你設置的屬性之後。但似乎你做你自己的查詢,這樣你就可以完全跳過這一行:

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

+0

Asped ......我懷念那個代碼,同時發佈...已編輯question..have一個look.And你建議在查詢之後包含搜索,對吧。? – saji