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

SS3 - how to set searchable fields for a dataobject


Go to End
Reply


13 Posts   3852 Views

Avatar
BenWu

Community Member, 92 Posts

25 July 2012 at 11:07pm

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.

Avatar
priithansen

Community Member, 25 Posts

26 July 2012 at 2:59pm

Edited: 26/07/2012 3:21pm

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

Avatar
BenWu

Community Member, 92 Posts

26 July 2012 at 8:34pm

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?

Avatar
priithansen

Community Member, 25 Posts

27 July 2012 at 12:25am

If I understand You correctly this should help.

Add to Contact Dataobject

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

Avatar
BenWu

Community Member, 92 Posts

27 July 2012 at 1:02am

Edited: 27/07/2012 1:03am

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

Avatar
priithansen

Community Member, 25 Posts

27 July 2012 at 1:19am

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?

Avatar
BenWu

Community Member, 92 Posts

27 July 2012 at 1:24am

you can get the files here https://github.com/sminnee/examplecrm/tree/master/mysite

Avatar
priithansen

Community Member, 25 Posts

27 July 2012 at 1:36am

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

Go to Top