Jump to:

1999 Posts in 1408 Topics by 613 members

Form Questions

SilverStripe Forums » Form Questions » SearchContext and Versioned : bad SQLQuery !?

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

Page: 1
Go to End
Author Topic: 1294 Views
  • Myrdhin
    Avatar
    Community Member
    67 Posts

    SearchContext and Versioned : bad SQLQuery !? Link to this post

    Hello,

    I try to use SearchContext in my project but it doesn't work :'(
    Silverstripe always add "_Live" to all table, even relation tables (many_many) to the query.

    My Code :

    + I have a "Level" (DataObject) with just a Title :

    class Level extends DataObject {

       static $db = array(
             'Title' => 'Varchar(255)'
       );

       static $belongs_many_many = array(
             'Documents' => 'Document'
       );

       function getCMSFields() {
          $fields = parent::getCMSFields();
          $fields->addFieldToTab(
             'Root.Main',
             new TextField('Title', $this->fieldLabel('Title'))
          );
          
          return $fields;
       }
       
       function listAll() {
          $objs = DataObject::get($this->class);

          if(!$objs) return array();

          $list = array_unique($objs->column('Title'));
          sort($list);
          return $list;
       }

    }

    + A Document class (extends Page) and a many_many relation with "Level". I define the getDefaultSearchContext method to change the Level field (to a dropdown field) :

    class Document extends Page {

       static $db = array(
             'Subtitle' => 'Varchar(255)'
       );

       static $many_many = array(
             'Levels' => 'Level'
       );

       static $searchable_fields = array(
             'Levels.Title'
       );

       static $field_labels = array(
             'Levels.Title' => 'Levels'
       );

       ...

       public function getDefaultSearchContext() {
          $context = parent::getDefaultSearchContext();

          $context->removeFieldByName('Levels');

          $levelMap = null;
          $levels = Singleton('Level')->listAll();
          if($levels) { $levelMap = array_combine($levels, $levels); }

          $levelField = new DropdownField( 'Levels', 'Levels', $levelMap );
          $levelField->setHasEmptyDefault(true);

          $context->addField($levelField);

          return $context;
       }
    }

    + And i created a new type of page to search Documents with Level.Title. i define a searchForm with the SearchContext of the "Document" :

    class DocumentSearch extends Page {
    }

    class DocumentSearch_Controller extends Page_Controller {

       function DocumentSearchForm() {
          $context = singleton('Document')->getDefaultSearchContext();
          $fields = $context->getSearchFields();

          $form = new Form($this, "DocumentSearchForm",
             $fields,
             new FieldSet(
                new FormAction('DocumentSearch', _t('MemberTableField.SEARCH'))
             )
          );
          
          return $form;
       }

       function DocumentSearch($data, $form, $request) {
          $searchCriteria = array(
             'Levels.Title' => $data['Levels']
          );

          return $this->render(
             array(
                'DocumentResults' => $this->DocumentSearchResults($searchCriteria),
                'DocumentSearchForm' => $form
             )
          );
       }

       function DocumentSearchResults($searchCriteria=array()) {
          $context = singleton('Document')->getDefaultSearchContext();
          $records = $context->getResults($searchCriteria);
          return $records;
       }

    }

    But when i search a document, Silverstripe give me an error :

    Couldn't run query: SELECT DISTINCT "SiteTree_Live" ... blabla... Table 'MyDB.Document_Levels_Live' doesn't exist.

    I have a "Document_Levels" table but not "Document_Levels_Live" !?

    Really, thanks four your help !

  • joern
    Avatar
    Community Member
    28 Posts

    Re: SearchContext and Versioned : bad SQLQuery !? Link to this post

    Same problem hereā€¦

  • markjames
    Avatar
    Community Member
    2 Posts

    Re: SearchContext and Versioned : bad SQLQuery !? Link to this post

    For anyone who hits this issue and finds this page as a result, I think the fix might be something like the following (untested) change to Versioned.php (ignore tables which are not names of classes that have the Versioned extension):

    https://gist.github.com/1828664

  • Dynamic
    Avatar
    Community Member
    16 Posts

    Re: SearchContext and Versioned : bad SQLQuery !? Link to this post

    Hey markjames,

    Just wanted to verify that your Versioned edit seems to do the trick. Thanks for the tip!

    1294 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.