my first feature and writing mvc

i am creating a new search feature as first feature on Yii to understand the flow and working of framework.

here is what i did.

  1. I created a model and extended CFormModel

  2. I defined variables and wrote a function for search using Query builder

code:

        public function searchQuery(){


        $command = Yii::app()->db->createCommand()     //code that do the search query


        ->select('*')


        ->from('tbl_restaurant_info')


        ->where('restaurant_name=:restaurant_name')    //I am not sure if i am doing this right!


        ->queryAll();
  1. Here is what i wrote in controller

Code

        $model = new SearchForm;


        if (isset($_POST['SearchForm']))


        {


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


        $viewdata = array(


            'dataprovider'=> $model->searchQuery(),       //I am trying to store the output of query  


        );


        





       $this->render('SearchResult',array('model'=>$viewdata));


    }


    else{


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


    }

4)In view I want to output the search result

code

      var_dump($model['dataprovider']);

I have an input form which takes the user input and is getting posted in $_POST[‘SearchForm’]

I see the output as null.

please help me understand where i am going wrong. I do not understand,where the output of search query being stored and how it will be retrieved and how to bind variable value in the where clause of sql.

Thanks.

Clearly searchQuery() is empty. Does it have a proper ‘return’?

Try:




$model = new SearchForm;

if (isset($_POST['SearchForm']))

{

  $data = $model->searchQuery();

  die(count($data));

}



Hi Spritespirit,

In addition what Backslider said I noticed that you need to add a binded value, e.g.


->where('restaurant_name=:restaurant_name', array(':restaurant_name'=>$restaurantName))

Of course you’ll need to have [color="#1C2837"][size=“2”]$restaurantName variable set beforehand.[/size][/color]

And please don’t forget to use code formatting next time. Simplifies reading ;)