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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

How to create an empty DropDownField ? Error in DataObjectManager.php on line 584


Go to End


3 Posts   1562 Views

Avatar
lise

Community Member, 47 Posts

3 April 2010 at 8:48pm

I populate a DropDownField automatically from an array as :

function getCMSFields() {

$fields = new FieldSet( new DropdownField(
'CountryName',
'Choose a country',
Dataobject::get("country")->toDropdownMap("CountryName", "CountryName")
), ..etc ..
}

This works very well when 'country' already contains data. However I get an error when at init. there
is **no country** yet in the database.

I understand this is because Dataobject::get() is NULL and therefore the call to toDropdownMap fails.

So I tried to use a function which tests if 'country' is empty or not :

function CreateCountryList() {
$myDataSet = DataObject::get("country");
if (!$myDataSet) return false;
else
return $myDataSet->toDropdownMap("CountryName", "CountryName");
}

$fields = new FieldSet( new DropdownField(
'CountryName',
'Choose a country',
$this->CreateCountryList()
),
etc...

But with that, I can not properly create a country for the customer with the DOM. (i.e the entry is written in the DB but the DOM
does not display it in the table and returns the error messages :

ERROR [Warning]: Missing argument 3 for ComplexTableField_Item::__construct(), called in /home/www/xxx.com/dataobject_manager/code/DataObjectManager.php on line 584 and defined
IN POST /admin/getitem?ID=11&ajax=1
Line 980 in /home/www/xxx.com/sapphire/forms/ComplexTableField.php

I guess my CreateCountryList() is wrong so my question is ...how to create an empty DropDownField ?

I hope this is not too confusing ...

Thanks in advance for any tip,

Lise

PS. I am running SS 2.3.7 with the latest DOM ( March 23 )

Avatar
UncleCheese

Forum Moderator, 4102 Posts

4 April 2010 at 2:21am

If you're running SS 2.3, you need to use the 2.3 branch of DOM. See the "2.3 branch" sticky.

Never, ever, ever run a function directly against a query. If it comes back false, you get a fatal error, like you're seeing.

$map = ($set = DataObject::get("MyThing")) ? $set->toDropdownMap() : array();

new DropdownField('Foo','Bar',$map);

Avatar
lise

Community Member, 47 Posts

4 April 2010 at 9:53am

Thank you very much for the tip : I used your code **and** I downloaded the 2.3 version of the DOM (I was running the 2.4 from the
Modules page ) and it works fine.
Thanks again - Lise