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.

Customising the CMS /

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

[Solved] Saving a $many_many in a dropdown field


Go to End
Reply


2 Posts   1124 Views

Avatar
Graphicator

Community Member, 62 Posts

19 January 2011 at 7:22pm

Edited: 19/01/2011 7:22pm

I hit a wall with saving the selected DropdownField while editing a Page. I'm able to grab the different titles, but nothing is saved.

I want to select one Title Column that is inside the Brands class extending DataObject.
I successfully implemented the dropdown field as set up in Chapter 6 of Silverstripe book to use as a search field. This time I'm applying to same way of generating the field but for adding it to a Page's field.

Item.php

public static $many_many = array(
		'Brands' => 'Brand'
	);

	function getCMSFields() {
...
	$brands = Product::get_brands();
		if($brands){
			$brandMap = array_combine($brands,$brands);
		} else {
			$brandMap = null;
		}
		$brandField = new DropdownField(
			'Brand',
			'Brand',
			$brandMap
		);
		$brandField->setHasEmptyDefault(true);
		
		$fields->addFieldToTab('Root.Content.Main',$brandField);

Item.php cont'd

	# 6.7.3 Drop-down Menu for Searching brands
	public function getDefaultSearchContext() {
		$context = parent::getDefaultSearchContext();
		$context->removeFieldByName('Brand');
		$brands = self::get_brands();
		if($brands){
			$brandMap = array_combine($brands,$brands);
		} else {
			$brandMap = null;
		}
		$brandField = new DropdownField(
			'Brand',
			'Brand',
			$brandMap
		);
		$brandField->setHasEmptyDefault(true);
		$context->addField($brandField);
		return $context;
	}
	
	public static function get_brands() {
		$comp = DataObject::get('Brand');
		if(!$comp) return array();
		$brands = array_unique($comp->column('Title'));
		sort($brands);
		return $brands;
	}

Brand.php

class Brand extends DataObject {
	static $db = array(
		'Title' => 'Varchar(255)' ,
	);
	static $belongs_many_many = array(
		'Products' => 'Product',
	);
	
	static $searchable_fields = array(
		'Title' => 'ExactMatchFilter'
	);
	
}

Avatar
Graphicator

Community Member, 62 Posts

19 January 2011 at 9:53pm

After banging my head a few times, I realized I was missing componentset.

Really needed to look at the datamodel page: http://doc.silverstripe.org/sapphire/en/topics/datamodel

And decided to comment out the belongs_many_many to a has_many for each brand.

Brand.php

	# static $belongs_many_many = array(
	#	'Products' => 'Product',
	# );
	static $has_many = array(
		'Products' => 'Product.Brands',
	);

The dropdown is saving now that I used
Product.php

	function getCMSFields() {
....
	$brandMap = Dataobject::get("Brand");
		$brandArray = $brandMap ->toDropdownMap("ID", "Title",'',true);
		$brandField = new DropdownField(
			'BrandsID',
			'Please choose a brand',
			$brandArray
		);
		$brandField->setHasEmptyDefault(true);
		$fields ->addFieldToTab('Root.Content.Main',$brandField,'Content');

Thank you!