Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » [SOLVED] FilteredDropdownSet - not filtered

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 1595 Views
  • Craftnet
    Avatar
    Community Member
    58 Posts

    [SOLVED] FilteredDropdownSet - not filtered Link to this post

    Hi,
    I have a problem with FilteredDropdownSet

    I have three DataObject
    Two is Province and Twon
    Third is Simple Object

    I have that code

    Province.php

    class Province extends DataObject
    {

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

    static $field_labels = array(
          'Title' => 'Województwo'
       );

    }

    and

    Town.php

    class Town extends DataObject
    {

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

    static $has_one = array (
    'Province' => 'Province',
    );

    static $has_many = array (
    'Anonse' => 'Anons',
    );

    static $field_labels = array(
          'Title' => 'Miasto'
       );
    }

    and DataObject Anons.php (he display in frontpage)

    class Anons extends DataObject
    {   

    .................
       
       static $has_one = array(
          'Province' => 'Province',
          'Town' => 'Town'
       );

       //Fields to search in ModelAdmin
       static $searchable_fields = array (
          'Province.ID' => array('title' => 'Województwo'),
          'Town.ID' => array('title' => 'Miasto'),
       );

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

    ....................      
          
          
          //Main Tab
          $fields->removeByName('Province');
          $fields->removeByName('Town');
          

          $province_map = array();
          $town_map = array();
          
          if($result = DataObject::get("Province")) {
       $province_map = $result->toDropdownMap();
    }
          
          if($result = DataObject::get("Town")) {
       $town_map = $result->toDropdownMap();
    }
          
          $fields->addFieldToTab("Root.Main", new FilteredDropdownSet(array(
             new DropdownField('ProvinceID', 'Province', $province_map),
             new DropdownField('TownID', 'Town', $town_map)
          ),
          "ProvinceID",
          "Town"
          ));
          
          return $fields;
       }

       .........................
       
       public function getCustomSearchContext() {
          $province_map = array();
          $town_map = array();
          
          if($result = DataObject::get("Province")) {
       $province_map = $result->toDropdownMap();
    }
          
          if($result = DataObject::get("Town")) {
       $town_map = $result->toDropdownMap();
    }    
          $fields = new FieldSet(
             new FilteredDropdownSet(array(
             new DropdownField('ProvinceID', 'Province', $province_map),
             new DropdownField('TownID', 'Town', $town_map)
          ),
          "ProvinceID",
          "Town"
          )
    );
    $filters = array(
    'Province' => new ExactMatchMultiFilter('Province.ID'),
    'Town' => new ExactMatchMultiFilter('Town.ID'),
    );
    return new SearchContext(
    $this->class,
    $fields,
    $filters
    );   

    }

    Neither in the frontend or backend filtering does not work.
    I mean when for example I point on province "lodzkie" in second field (Town) not filtered - allways display all Twon

    Sorry for my bad English

  • pedro2555
    Avatar
    Community Member
    2 Posts

    Re: [SOLVED] FilteredDropdownSet - not filtered Link to this post

    The problem is in the FieldSet. FilteredDropdownSet does not work inside a FieldSet nor a FieldGroup. I had that same problem, f*ck*ng nightmare. Try it out like this :

    $filter = new FilteredDropdownSet(array(
    new DropdownField('ProvinceID', 'Province', $province_map),
    new DropdownField('TownID', 'Town', $town_map)
    ),
    "ProvinceID",
    "Town"
    );

    $fields->addFieldToTab("Root.Main", $filter);

    Be sure to fully populate the $province_map and $town_map.

    Cheers, hope it works.

  • Craftnet
    Avatar
    Community Member
    58 Posts

    Re: [SOLVED] FilteredDropdownSet - not filtered Link to this post

    Hi,
    I did not have when check this

    Looks like it works
    Many thank's

    You are right - this is f*ck*ng nightmare

    Sorry for bad English

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