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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

[SOLVED] FilteredDropdownSet - not filtered


Go to End


3 Posts   2373 Views

Avatar
Craftnet

Community Member, 58 Posts

19 February 2012 at 1:32pm

Edited: 19/02/2012 1:42pm

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

Avatar
pedro2555

Community Member, 2 Posts

3 May 2012 at 11:30pm

Edited: 03/05/2012 11:30pm

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.

Avatar
Craftnet

Community Member, 58 Posts

17 October 2012 at 1:47pm

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