Jump to:

23493 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » SS3 - how to set searchable fields for a dataobject

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

Page: 1 2
Go to End
Author Topic: 3347 Views
  • BenWu
    Avatar
    Community Member
    90 Posts

    SS3 - how to set searchable fields for a dataobject Link to this post

    Hello,

    I got a simple class

    class Contact extends DataObject {
    static $db= array('Firstname'=>'varchar', 'Surname'=>'varchar');

    static $has_one = array(
    "Company" => "Company",
    );

    static $many_many = array(
    "Leads" => "Lead",
    );

    }


    Then in the Leads Admin page, when i try to link it to a contact, i got this error:

    Error at framework/forms/gridfield/GridFieldAddExistingAutocompleter.php line 171: Uncaught LogicException: GridFieldAddExistingAutocompleter: No searchable fields could be found for class "Contact"

    If I replace 'Firstname' with Name, I don't have problem at all. So by default, GridFieldAddExistingAutocompleter tries to use the 'Name' field to search for auto-completion, if there is no Name fields, it will fail.

    I tried to put

    static $searchable_fields = array('Firstname', 'Surname');


    but it doesn't help.

  • priithansen
    Avatar
    Community Member
    25 Posts

    Re: SS3 - how to set searchable fields for a dataobject Link to this post

    It appears to me that the gridfield only looks for Title or Name field by default.

    So adding something like this to GridField code should solve half the battle.

          $config = GridFieldConfig_RelationEditor::create();
          $config->getComponentByType('GridFieldAddExistingAutocompleter')->setSearchFields(array('Firstname', 'Surname'))->setResultsFormat('$Firstname - $Surname');
          $gridField = new GridField("Contact", "Contact", $this-> Contact(), $config);
       $fields->addFieldToTab("Root.Main", $gridField);

  • BenWu
    Avatar
    Community Member
    90 Posts

    Re: SS3 - how to set searchable fields for a dataobject Link to this post

    many thanks.

    I found something simliar but not sure where to put them. I am using the ModelAdmin to manage Contact.

    class ContactAdmin extends ModelAdmin {
    static $menu_title = "Contacts";
    static $url_segment = "contacts";

    static $managed_models = array(
    "Contact",
    );
    }

    The search result of ModelAdmin is displayed in a Gridfield. Is it possible to get that Gridfield and tell it what the searchable fields are?

  • priithansen
    Avatar
    Community Member
    25 Posts

    Re: SS3 - how to set searchable fields for a dataobject Link to this post

    If I understand You correctly this should help.

    Add to Contact Dataobject

    static $searchable_fields = array(
          'CompanyID' => array('title' => 'Company'),
          'LeadsID' => array('title' => 'Leads')
    );

  • BenWu
    Avatar
    Community Member
    90 Posts

    Re: SS3 - how to set searchable fields for a dataobject Link to this post

    Thanks.

    The error shows up when I edit a Lead in the LeadAdmin and tried to link a Lead to an existing contact.

    <?php

    class LeadAdmin extends ModelAdmin {
       static $menu_title = "Leads";
       static $url_segment = "leads";

       static $managed_models = array(
          "Lead",
       );
    }

    class Lead extends DataObject {
       static $db = array(
          "Name" => "Varchar(255)",
          'Content'=>'HTMLText',
          "Status" => "Enum('Open,Won,Lost,Cancelled','Open')",
       );

       static $has_one = array(
          "Company" => "Company",
       'Creator'=>'Member',
       );

       static $belongs_many_many = array(
          "Contacts" => "Contact",
       );
    }

    Therefore, the 'Firstname' and 'Surname' of the Contact object needs to be searchable.

    Currently, I have to add a field called Name and in the Contact class, I did this to sync Name to firstname and surname

       public function onAfterWrite(){
        parent::onAfterWrite();
        $this->Name = $this->getName();
        $this->Name = str_replace("'", '"', $this->Name);
        DB::query('UPDATE "'. $this->baseTable(). '" set name =\''. $this->Name . '\' where ID = '. $this->ID );
       }

    public function getName() {
    return ($this->Surname) ? trim($this->FirstName . ' ' . $this->Surname) : $this->FirstName;
    }


    but it is not an idea solution as I have duplicated content in the database

    regards,
    Ben

  • priithansen
    Avatar
    Community Member
    25 Posts

    Re: SS3 - how to set searchable fields for a dataobject Link to this post

    Couldn't you overwrite the default gridfield for lead in the Lead DataObject with

    public function getCMSFields() {
          $fields = parent::getCMSFields();

          //Gridfield with the same name as default.

          return $fields;
       }

    Maybe You can send me the files so I can see first hand what the problem is exactly?

  • BenWu
    Avatar
    Community Member
    90 Posts
  • priithansen
    Avatar
    Community Member
    25 Posts

    Re: SS3 - how to set searchable fields for a dataobject Link to this post

    This seems to work for me

    <?php
    class Lead extends DataObject {
       static $db = array(
          "Name" => "Varchar",
          "Status" => "Enum('Open,Won,Lost,Cancelled','Open')",
       );

       static $has_one = array(
          "Company" => "Company",
       );

       static $belongs_many_many = array(
          "Contacts" => "Contact",
       );
       public function getCMSFields() {
          $fields = parent::getCMSFields();

       $config = GridFieldConfig_RelationEditor::create();
       $config->getComponentByType('GridFieldAddExistingAutocompleter')->setSearchFields(array('FirstName', 'Surname'))->setResultsFormat('$FirstName - $Surname');
       $gridField = new GridField("Contacts", "Contacts", $this-> Contacts(), $config);
       $fields->addFieldToTab("Root.Contacts", $gridField);

       return $fields;
       }
    }

    3347 Views
Page: 1 2
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.