2014-04-04 32 views
0

我是yii框架中的新功能。我正在做數據庫中的重新搜索並使用yii框架在另一個頁面中顯示。我的控制器是Sitecontroller.php,查看頁面是search.php和search_result .PHP。我的模型是job.php.My錯誤是 「urlencode()期望參數1是字符串,對象給出使用yii框架在搜索和顯示記錄中的錯誤

C:\ wamp \ www \ yii \ framework \ web \ CUrlManager.php(440)」。

我控制器Sitecontroler.php

<?php 
class SiteController extends Controller 
{ 
public function actionsearch() 
    { 
    $user_id = trim($_GET['id']); 
    $model = new Job ; 
    if(isset($_POST['Job'])) 
     { 
     $model->attributes=$_POST['Job']; 
     $title=$_POST['Job']['title']; 
     $title=trim($title); 
     $model=Job::model()->find(array('select'=>'*',"condition"=>"title like '%$title%'",)); 
     $number=count($model); 
     if($number>0) 
     { 
      $this->redirect($this->createUrl('site/search_result',array('title'=>$title)));  
     } 

     } 
    $this->render('search',array('model' =>$model)); 
} 
public function actionsearch_result() 
    { 
    $title=$_GET['title']; 
    $model = new Job ; 
    $model=Job::model()->find(array('select'=>'*',"condition"=>"title like '%$title%'",)); 
    $this->render('search_result',array('model' =>$model)); 
} 
}?> 

我的視圖文件,search.php中

<div class="form"> 
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form', 
'enableClientValidation'=>false, 
'htmlOptions' => array(), 
    'clientOptions'=>array(
    'validateOnSubmit'=>true 
), 
)); ?> 
<?php 
foreach(Yii::app()->user->getFlashes() as $key => $message) { 
    echo '<div class="flash-' . $key . '">' . $message . "</div>\n"; 
} 
?> 
    <div class="row"> 
    <?php echo $form->labelEx($model,'Keyword'); ?> 

    <?php echo $form->textField($model,'title'); ?> 
    <?php echo $form->error($model,'title'); ?> 
</div> 
<div class="row buttons"> 
    <?php echo CHtml::submitButton('Search'); ?> 
</div> 
<?php $this->endWidget(); ?> 
</div> 

視圖文件search_result.php

<div style="float:right;margin-right:285px;"> 
<h1>Search Jobs</h1> 
<table width="200" border="1"> 
<tr> 
<td>SI No</td> 
<td>Title</td> 
<td>Key Skill</td> 
<td>Experince</td> 
</tr> 
<?php 
foreach($model as $models) 
{ 
?> 
<tr> 
<td></td> 
<td><?php echo $models->title; ?></td> 
<td><?php echo $models->key_skills; ?></td 
><td><?php echo $models->experience; ?></td 
></tr> 
<?php 
} 
?> 
</table> 
</div> 

任何機構幫助我嗎?

回答

0

有在這一行的問題

$this->redirect(array('site/Search_result',array('model' =>$model))); 

你應該試試這個代替

$this->redirect($this->createUrl('site/search_result',array('parameter'=>'value'))); 

注: - 你不能通過URL發送$模式,因爲它是一個對象,這就是爲什麼你得到這個錯誤。

UPDATE

在你search_result。PHP

改變這個代碼

foreach($model as $models) 
{ 
?> 
<tr> 
<td></td> 
<td><?php echo $models->title; ?></td> 
<td><?php echo $models->key_skills; ?></td 
><td><?php echo $models->experience; ?></td 
></tr> 
<?php 
} 

<tr> 
<td></td> 
<td><?php echo $model->title; ?></td> 
<td><?php echo $model->key_skills; ?></td 
><td><?php echo $model->experience; ?></td 
></tr> 

闡釋

在這一行

$model=Job::model()->find(array('select'=>'*',"condition"=>"title like '%$title%'",)); 

正如你可以看到你已經使用find()來獲取數據。所以find()會返回一行(因此是單個對象)而不是一個對象數組。如果你想,然後獲取多條記錄,你應該使用的findAll(當使用的findAll()

的話)你的代碼變得

$model=Job::model()->findAll(array('select'=>'*',"condition"=>"title like '%$title%'",)); 

,然後你可以根據你的使用使用相同的代碼search_result.php文件中像

foreach($model as $models) 
{ 
?> 
<tr> 
<td></td> 
<td><?php echo $models->title; ?></td> 
<td><?php echo $models->key_skills; ?></td 
><td><?php echo $models->experience; ?></td 
></tr> 
<?php 
} 
+0

我已經改變了控制器的代碼。請看看它。現在顯示錯誤嘗試獲取非物體的屬性 C:\ wamp \ www \ yii \ jobsite_orginal \ protected \ views \ site \ search_result.php(26) – user3475251

+0

你能告訴哪一行是第26行嗎? –

+0

​​<?php echo $ models-> title; ?>在search_result.php – user3475251

0

我認爲問題可能在於您的行爲命名錯誤。 你應該使用camelCase

public function actionSearch() 
... 
public function actionSearch_result() 

這是被Yii所需的命名,所以它可以找到你的行動。 redirect方法需要一個行動,沒有這個,我想它找不到它。

而且重定向期待controler/action路線,這意味着在你的情況應該是:

$this->redirect(array('site/search_result','model' =>$model)); 

請記住,該函數actionSearch_result與資本小號但路線本身,是site/search_result以小寫小號

+0

請看看我的控制器代碼,我不得不改變it.again顯示了這同樣的錯誤 – user3475251

+0

你仍然錯了,請參閱我的更新 - 你應該改名只有函數名和..另外一個所以'模型 - > $模型'不應該在一個單獨的數組中。請檢查文檔http://www.yiiframework.com/doc/api/1.1/CController#redirect-detail – Asped

+0

如果操作名稱中有下劃線或小寫字母「s」,則無關緊要。 –