我是yii框架中的新手。我正在使用yii framework在数据库中进行搜索,并在另一个页面中显示。我的控制器是Sitecontroller.php,查看页面是search.php和search_result.php。我的模型是job.php.My错误是 “urlencode()期望参数1为字符串,给定对象
C:\瓦帕\ WWW \ YII \框架\幅\ 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));
}
}?>
我的视图files-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>
查看file-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 :(得分:0)
我认为问题可能是,你的行为命名不好。
您应该使用camelCase
:
public function actionSearch()
...
public function actionSearch_result()
这是Yii所需的命名,因此它可以找到您的行动。
redirect
方法需要一个动作,没有这个我想它找不到它。
此外,重定向需要controler/action
路由,这意味着在您的情况下应该是:
$this->redirect(array('site/search_result','model' =>$model));
请注意,该功能是使用Capital S 的actionSearch_result,但路线本身为site/search_result
,小写 s
答案 1 :(得分:0)
这一行存在问题
$this->redirect(array('site/Search_result',array('model' =>$model)));
你应该试试这个
$this->redirect($this->createUrl('site/search_result',array('parameter'=>'value')));
注意: - 您无法通过网址发送$ model,因为它是一个对象,这就是您收到此错误的原因。
<强>更新强>
在你的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
}