我是yii框架中的新手。我正在尝试使用yii框架进行搜索操作。我有多个条件。
我收到了错误。 尝试分配非对象的属性
C:\瓦帕\ WWW \ YII \框架\幅\ CPagination.php(216)
有人帮帮我吗?
我的控制器代码是
<?php
public function actionadvance_search_result()
{
$title=$_GET['title'];
$experience=$_GET['experience'];
$location=$_GET['location'];
$criteria=new CDbCriteria();
$criteria_new =NULL;
if($experience!="")
{
$criteria_new = $criteria->condition = "(title like '%$title%' or key_skills like '%$title%') and (experience like '%$experience%')";
}
if($location!="")
{
$cri_location=$criteria->condition = "location_id like '%$location%'";
$criteria_new = $cri_location.' '.'and'.' '.$criteria_new;
}
$count=Job::model()->count($criteria_new);
$pages=new CPagination($count);
$pages->pageSize=2;
$pages->applyLimit($criteria_new);
$model=Job::model()->findAll($criteria_new);
$number_rec=count($model);
if($number_rec<=0)
{
$this->render('search_result1',array('model' =>$model));
}
else
{
$this->render('advance_search_result',array('model' =>$model,'pages' => $pages));
}
}
?>
答案 0 :(得分:0)
我没有得到你想要实现的目标,但使用$criteria_new
变量似乎是一个问题并且是多余的。
在这里使用$criteria->addCondition()
。
<?php
public function actionadvance_search_result()
{
$title=$_GET['title'];
$experience=$_GET['experience'];
$location=$_GET['location'];
$criteria = new CDbCriteria();
if($experience!="")
{
$criteria->addCondition("(title like '%$title%' or key_skills like '%$title%') and (experience like '%$experience%')");
}
if($location!="")
{
$criteria->addCondition("location_id like '%$location%'");
}
$count=Job::model()->count($criteria);
$pages=new CPagination($count);
$pages->pageSize=2;
$pages->applyLimit($criteria;
$model=Job::model()->findAll($criteria);
$number_rec=count($model);
if($number_rec<=0)
{
$this->render('search_result1',array('model' =>$model));
}
else
{
$this->render('advance_search_result',array('model' =>$model,'pages' => $pages));
}
}
答案 1 :(得分:0)
我希望$ criteria_new
中存在问题当$ experience为NULL时,它将形成不完整的查询。
试试这个
$criteria=new CDbCriteria();
// $criteria_new =NULL; ---------> Remove it
$condition='';
if($experience!="")
{
$condition=(title like '%$title%' or key_skills like '%$title%') and (experience like '%$experience%');
}
if($location!="")
{
if($experience!="")
{
$tempCond="location_id like '%$location%'";
$condition=$condition.' AND '. $tempCond;
}
else
{
$condition=$tempCond;
}
}
现在applyLimit()应该是
$pages->applyLimit($criteria);