Jump to:

2011 Posts in 1436 Topics by 620 members

Form Questions

SilverStripe Forums » Form Questions » SearchForm returning strange results

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

Page: 1
Go to End
Author Topic: 1050 Views
  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    SearchForm returning strange results Link to this post

    Hello.

    I've just followed the tutorial for site search and I am getting some strange results from the search.
    The strange thing is that the results I get back don't act as if they're the class they're supposed to be.

    For example, I have a ProductPage that extends Page which, among other things, contains an Image through a has_one.

    When I do a search on one of the products I get a DataObjectSet back with a ProductPage DataObject and as far as I can see, all the data for the page is correct. However if I do an attempt to get the image I get an empty image object, not the image related to the product.

    I found a way around this by looping through the search results and doing

    $results = $form->getResults();
    $searchData = new DataObjectSet;
    foreach ($results as $result) {
       $searchData->push(DataObject::get_by_id($result->ClassName, (int)$result->ID));
    }
    $data['Results'] = $searchData;


    And then returning $searchData to the template. Now I get access to the correct images and stuff. However this is essentially fetching the same results twice so if there's an easier way to make the search function return the right stuff I would be very happy if someone could tell me what to do.

    Kindly, Marcus.

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: SearchForm returning strange results Link to this post

    hi

    could you show the complete search code and template code ?

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: SearchForm returning strange results Link to this post

    ok i got it

       function results($data, $form){
       
          if($form->getSearchQuery() == NULL || $form->getSearchQuery() == 'Znajdź produkt') {
             $data = array(
                'Results' => '',
                'Query' => '',
                'Title' => 'Znalezione wyniki'
             );         
          } else {         
             $results = $form->getResults();
             $searchData = new DataObjectSet;
             foreach ($results as $result) {
                $searchData->push(DataObject::get_by_id($result->ClassName, (int)$result->ID));
             }   
             $data = array (
                'Results' => $searchData,
                'Query' => $form->getSearchQuery(),
                'Title' => 'Znalezione wyniki'            
             );
          }
          
           return $this->customise($data)->renderWith(array('Wyszukaj', 'Wyszukaj'));
       }

    1050 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.