Yii中的自定义提前搜索问题

时间:2014-03-21 11:52:38

标签: php yii bootstrapping

我在Yii实施了我的项目。我做了搜索部分工作正常。但我想要提前搜索。我得到的所有值除了表中的一个字段。请建议我需要改变的地方。我在这里添加了我的代码。

我的控制器部分:

public function actionAdvancesearch()
    {
        if(isset($_POST['Recipe']))
        $as=$_POST['Recipe']['recipe_name'];
        //var_dump($as);
        $model=new Recipe;
        if(isset($_POST['Recipe']))
           {

            $name=$_POST['Recipe']['recipe_name'];
            $course=$_POST['Recipe']['course_id'];
            $cuisine=$_POST['Recipe']['cuisinename'];
            $type=$_POST['Recipe']['type'];
            $calorie=$_POST['Recipe']['calorie_count'];

        $this->redirect(array('advancesearch1','course'=>$course,'cuisine'=>$cuisine,'name'=>$name,'calorie'=>$calorie,'type'=>$type));
           }

     $this->render('advancesearch',array('model'=>$model
            ));    
        }

public function actionAdvancesearch1()
    {
            $model=new Recipe;
            $name1=$_GET['name'];
            $course1=$_GET['course'];
            $cuisine1=$_GET['cuisine'];
            $type1=$_GET['type'];
            $calorie1=$_GET['calorie'];

    $model->unsetAttributes();  // clear any default values
    if(isset($_GET['Recipe']))
    $model->attributes=$_GET['Recipe'];

            $this->render('advancesearch1',array( 'course1'=>$course1,'cuisine1'=>$cuisine1,
            'name1'=>$name1, 'type1'=>$type1,
                'calorie1'=>$calorie1
                    ));

   }

我的视图部分只是打印值,但名称字段只显示空值..

我还添加了我的网址值

http://localhost/kitchenking/index.php/recipe/advancesearch1/course/3/cuisine/3/name//calorie/1/type/1

上面的URL名称值为空..不从表中获取。并且还尝试了var_dump()..其中也说该名称返回NULL值..

2 个答案:

答案 0 :(得分:2)

public function actionAdvancesearch1() {

   $model=new Recipe('search');
   $model->unsetAttributes();

   // do stuff here 

   $this->render('advancesearch1', etc....

}

使用搜索时不要忘记unsetAttributes

答案 1 :(得分:0)

要搜索您的模型,您需要使用“搜索”方案

变化

public function actionAdvancesearch1()
    {
            $model=new Recipe;

public function actionAdvancesearch1()
    {
            $model=new Recipe('search') ;
相关问题