Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Search Form for certain class


Reply


2 Posts   1690 Views

Avatar
Tonyair

Community Member, 81 Posts

25 June 2010 at 2:32am

Edited: 26/06/2010 4:15pm

Hello, in the tutorial 4 described how to create search form, but this is form searches around all DB, i found this recipe:
http://doc.silverstripe.org/recipes:removingassetsfromsearch?s[]=getsearchquery

But it looks very dirty and results counting variables are staying the same.

So after reading silverstripe api, i found out that i can use FulltextSearchable::enable($class_names_array), but after adding it to the mysite/_config.php I got error on http://localhost/stable/dev/build?flush=1:

Fatal error: Uncaught exception 'Exception' with message 'FulltextSearchable::enable() I don't know the default search columns for class 'Page'' in /home/a2nt/work/www/stable/sapphire/search/FulltextSearchable.php:25 Stack trace: #0 /home/a2nt/work/www/stable/mysite/_config.php(48): FulltextSearchable::enable('Page') #1 /tmp/silverstripe-cache-home-a2nt-work-www-stable/manifest-main(6783): require_once('/home/a2nt/work...') #2 /home/a2nt/work/www/stable/sapphire/core/ManifestBuilder.php(66): require_once('/tmp/silverstri...') #3 /home/a2nt/work/www/stable/sapphire/core/Core.php(205): ManifestBuilder::include_manifest() #4 /home/a2nt/work/www/stable/sapphire/main.php(61): require_once('/home/a2nt/work...') #5 {main} thrown in /home/a2nt/work/www/stable/sapphire/search/FulltextSearchable.php on line 25

So I added to mysite/code/Page.php

class Page extends SiteTree {
..
   static $indexes = array(
      "SearchFields" => "fulltext (Title, MenuTitle, Content, MetaTitle, MetaDescription, MetaKeywords)",
      "TitleSearchFields" => "fulltext (Title)",
      "Sort" => true,
      "Uniques" => "Unique('URLSegment')",
   );
   static $searchable_fields = array(
      'Title',
      'Content',
   );
..

But still I have that error, also I am interested in creation different types of search forms for example to search only in StaffPage classes.

Avatar
Tonyair

Community Member, 81 Posts

26 June 2010 at 3:05am

Edited: 26/06/2010 4:15pm

Problem partly solved:
I can search by one field, but i want search with OR-condition by many fields so I tried to write that code:

   static $searchable_fields = array(
      'Query' => array (   
         'Title' => "PartialMatchFilter",
         'Content' => "PartialMatchFilter",
         'MenuTitle' => "PartialMatchFilter",
         'Content' => "PartialMatchFilter",
         'URLSegment' => "PartialMatchFilter"
      )
   );

but it's not working.
If I'll write: $context->getResults(array('Content' => $data['Query'],'Title' => $data['Query'])); I'll get search with AND-condition

Now my code looks like that:

class Page:

   static $searchable_fields = array(
      
         'Title' => "PartialMatchFilter",
         'Content' => "PartialMatchFilter",
         'MenuTitle' => "PartialMatchFilter",
         'Content' => "PartialMatchFilter",
         'URLSegment' => "PartialMatchFilter"
      
   );
   //Show in search results
   static $summary_fields = array(
      'Title',
      'Menu',
      'Content'
   );

class Page_Controller:

public function AdvSearchForm() {
      // Create fields
      $fields = new FieldSet(
         new TextField('Query'),
         new CheckboxSetField( 'Types', 'Search in',
            array(
               'Page' => 'Pages',
               'Staff' => 'Staffs',
               'News' => 'News'
            ), 'Page' )
      );
      // Create actions
      $actions = new FieldSet(
         new FormAction('AdvSearchResults', 'Submit')
      );
      $validator = new RequiredFields('Name','Types');
      return new Form($this, 'AdvSearchForm', $fields, $actions,$validator);
   }

   function AdvSearchResults($data, $form) {
      //$data['Query'];
      
      foreach ($data['Types'] as &$type) {
         switch ($type) {
            case 'Page':
               $context = singleton($type)->getDefaultSearchContext();
               $data[$type] = $context->getResults(array('Content' => $data['Query']));
            break;
            case 'Staff':
            break;
            case 'News':
            break;
         }
      }

      return $this->customise($data)->renderWith(array('Page_AdvSearch', 'Page'));
   }

P.S. Plz, move the topic out from template questions