2

I have two DataObjects named AosMember and Council.

The Council class has a $has_one relationship to AosMember.

class Council extends DataObject {

    public static $db = array(
        'Title' => 'Varchar(255)',
        'Year' => 'Year(4)'
    );

    public static $has_one = array(
        'Member' => 'AosMember'
    );

    public static $summary_fields = array(
        'Year',
        'Title'
    );

    private static $searchable_fields = array(
        'Member.LAST_NAME' => 'PartialMatchFilter'
    );
}

When I go into the ModelAdmin for Council in the CMS I see a text input for Member. The member count is +500 so ideally I want to be able to type in the last name and get an autocomplete of some kind.

At the very least I would like a drop down field. But I cannot find instructions on how to get either.

3dgoo
  • 15,716
  • 6
  • 46
  • 58
silversunhunter
  • 1,219
  • 2
  • 12
  • 32

3 Answers3

3

Adding Member.ID to the searchable fields list will make this field an autocomplete dropdown field:

class Council extends DataObject {
    // ...

    private static $searchable_fields = array(
        'Member.ID' => array(
            'title' => 'Member',
            'filter' => 'ExactMatchFilter'
        )
    );
}

The text to search on in the field will be the title of the AosMember object. If the AosMember doesn't have a title then we can return specific content through a getTitle function:

class AosMember extends DataObject {
    // ...

    public function getTitle() {
        return $this->FirstName . ' ' . $this->LastName;
    }
}
3dgoo
  • 15,716
  • 6
  • 46
  • 58
  • Hi. This is Great! It gives me the autocomplete in the search area which is close. When I click "Add Council" button it takes me to a form and there is a field for Aos Member as expected. This accepts numbers only which i presume would be the ID of the member. However it does not have the autocomplete search feature. CAn I somehow take what you have and apply it to that field? – silversunhunter Jun 22 '16 at 21:11
  • I think I would put the Member.ID array into the model admin. Trying to google a way to do that. – silversunhunter Jun 22 '16 at 21:15
  • @silversunhunter I'd suggest on looking at my answer first before googling. – Olli Tyynelä Jun 23 '16 at 13:52
2

You need to give SS some more info in your $searchable_fields static and a call to scaffoldSearchFields(). Something like this (I ripped this out of one of my own projects, you might need to alter it slightly for your own uses)

/**
 * @var array
 */
private static $searchable_fields = array(
    'MyField' => array(
        'title'     => 'Title',
        'field'     => 'DropdownField',
        'filter'    => 'PartialMatchFilter'
    )
);

Then you need to instruct SS how to scaffold your searchable Member field:

    public function scaffoldSearchFields($_params = null) {
        $fields = parent::scaffoldSearchFields();
        $field = DropdownField::create('MyField','My field search', Member::get()->map()->toArray());
        $fields->replaceField('MyField', $field);

        return $fields;
    }

Once it's working, you should see a DropdownField in the CMS' centre "filter" pane. N.b. You will prob need some further logic to deal with the has_one relation. The above example as-is, will only work for direct fields defined via $db.

theruss
  • 1,690
  • 1
  • 12
  • 18
  • I think that Searchable Fields is not what I am looking for. When I create a new Council field for the Member is just a text input. Shouldn't it be allowing me to find an actual member in the database to associate? – silversunhunter Jun 20 '16 at 23:08
  • Do you mean Council "field" or Council "object"? Show us the code that you have so far. – theruss Jun 21 '16 at 08:48
2

I'm presuming that you are letting the model admin to scaffold what editor it should use for managing has one relations.

It would be a dropdown field but if there are more than 100 records in will default to a input field for memory usage purposes see comments on the accepted answer: Silverstripe admin: "Has one" dropdown converts to ordinary input field after import

I'd suggest on using these as an alternative way to manage the has-one relation:

Haven't tested any of them but I'd assume the first one would be the best for your case.

Community
  • 1
  • 1
Olli Tyynelä
  • 586
  • 3
  • 14
  • Thanks FinBoWa.. I had visited that Stack link several times and tried to implement it. Finally today I got it to work. Not sure why it was so hard for me to figure out. I am going to edit your answer and put in the relevant code. – silversunhunter Jun 23 '16 at 17:53
  • @silversunhunter I think you shouldn't edit my answer. I'ts good as it is because it answers your issue albeit with links :) – Olli Tyynelä Jun 27 '16 at 10:05