[EXTENSION] My first try: Add a "search" field to the Blog-Demo

Hello everyone,

i am still a Yii-Beginner and i was trying to write an small search-portlet as an extension to the blog-demo.

At first, i added a SiteSearchPortlet.php to protected/components :

protected/components/SiteSearchPortlet.php:



<?php





class SiteSearchPortlet extends Portlet {


    


    public function renderContent() {


                $form = new SiteSearchForm();


                $this->render('siteSearch',array('form'=>$form));


    }


}


 ?>


Then i added a small view for this Portlet:

protected/components/views/siteSearch.php :



<?php echo CHtml::beginForm(); ?>


<div class="row">


<?php echo CHtml::activeTextField($form,'search_string') ?>


<?php echo CHtml::error($form,'search_string'); ?>


<?php echo CHtml::submitButton('Start Search'); ?>


</div>


<?php echo CHtml::endForm(); ?>





At last, i wanted to set up the Model Logic:

protected/models/SiteSearchForm.php :



<?php





class SiteSearchForm extends CFormModel


{


        public $search_string;





        public function rules() {


                return array( array('searchstring', 'required'),);


        }





        public function actionSearch() {


                $condition = new CdbCriteria;


                $params = array();


                $posts=Post::model()->findAll($condition,$params);


          print_r($posts); // Stuck here





} 





}


Now i have some trouble to understand, how Yii determines, which function is called, after the Submit-Button is pushed. After that, i just want to display these posts, that have ‘content like “%search_string%”’ as a CdbCriteria. Could some of you Yii-Experts give me an hint ;)

You'd better write an actionSearch in the appropriate controller, which in this case can be the PostController.

May be you do not wanna see, here is an example implementation.

http://code.google.c…rce/detail?r=19

Ah, ok! Thanks a lot. I really did not find this great extended blog demo Example.

It works now, but i needed to adjust the views/post/list.php a little bit:



<?php if(!empty($_POST['SiteSearchForm'])) { ?>





<h3>Posts that contain "<?php





$temparray = $_POST['SiteSearchForm'];


$searchstring = $temparray['search_string'];





echo CHtml::encode($searchstring); ?>":</h3>





<ul>


<?php


  foreach($posts as $post) {


    echo "<li><b>" . CHtml::link($post->title,array('show','id'=>$post->id)) ."</b> <br> $post->preview</li>";


  }


?>


</ul>


</div>


<?php


} else {





foreach($posts as $post) {


  $this->renderPartial('_post',array( 'post'=>$post,));


}


echo "<br/>";


$this->widget('CLinkPager',array('pages'=>$pages)); } ?>





Oh, it seems like the Formvalidator does not work. I can click "search" without entering anything?

Did the author of the extended blog demo forget about that, or is something broken with my installation??