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

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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

#1 User is offline   thyseus 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 300
  • Joined: 18-April 09
  • Location:Leipzig, Germany

Posted 13 May 2009 - 07:10 PM

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 ;)

0

#2 User is offline   mocapapa 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 428
  • Joined: 04-January 09
  • Location:Tokyo, JAPAN

Posted 13 May 2009 - 08:55 PM

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

#3 User is offline   mocapapa 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 428
  • Joined: 04-January 09
  • Location:Tokyo, JAPAN

Posted 15 May 2009 - 04:09 AM

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

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

#4 User is offline   thyseus 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 300
  • Joined: 18-April 09
  • Location:Leipzig, Germany

Posted 15 May 2009 - 06:24 AM

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??
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users