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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

SearchContext


Go to End


3 Posts   839 Views

Avatar
tomjohn

Community Member, 19 Posts

26 June 2015 at 7:20am

I'm using instructions form page http://docs.silverstripe.org/en/3.1/developer_guides/search/searchcontext/

I've got code:

<?php

class MyDataObject extends DataObject {

    private static $db = array(
        'PublicProperty' => 'Text'
        'HiddenProperty' => 'Text',
        'MyDate' => 'Int'
    );

    public function getCustomSearchContext() {
        $fields = $this->scaffoldSearchFields(array(
            'restrictFields' => array('PublicProperty','MyDate')
        ));

        $filters = array(
            'PublicProperty' => new PartialMatchFilter('PublicProperty'),
            'MyDate' => new GreaterThanFilter('MyDate')
        );

        return new SearchContext(
            $this->class, 
            $fields, 
            $filters
        );
    }
}
 

My questions are:
1. How can I add next field as bellow:
'MyDate' => new LessThanFilter('MyDate') ?

I added this but without effects. Maybe is other solution to make field with values from to?

2. How can I change labels name?

Avatar
Pyromanik

Community Member, 419 Posts

26 June 2015 at 11:02am

It's easier to configure the dataobject - there are configuration settings for these options. Something like 'searchable_fields' which provides an array for field, filter & label.

If you search the api you should find it. My battery is running out so I can't find link sorry.

Avatar
tomjohn

Community Member, 19 Posts

27 June 2015 at 1:39am

Thanks for reply. It was usefull. My working code on dataobject page is:

public function getCustomSearchContext() {
$fields = new FieldList();
$fields->push(new TextField('FirstName', 'Name'));
$fields->push(new TextField('BirthYear', '1'));
$fields->push(new TextField('BirthYear2', '2'));
$fields->push(new DropdownField(
'DeathDate',
'Choose a town',
array(
'1900' => '1900',
'1930' => '1930',
'1960' => '1960',
'1990' => '1990'
)));
$filters = array(
'FirstName' => new PartialMatchFilter('FirstName'),
'BirthYear' => new GreaterThanOrEqualFilter('BirthYear'),
'DeathDate' => new GreaterThanOrEqualFilter('DeathDate'),
'BirthYear2' => new LessThanOrEqualFilter('BirthYear')
);

return new SearchContext(
$this->class,
$fields,
$filters
);
}