5102 Posts in 1520 Topics by 1116 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 709 Views |
-
[Solved] Saving a $many_many in a dropdown field

19 January 2011 at 7:22pm Last edited: 19 January 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'
);
} -
Re: [Solved] Saving a $many_many in a dropdown field

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.phpfunction 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!
| 709 Views | ||
|
Page:
1
|
Go to Top |

