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.

DataObjectManager Module

Discuss the DataObjectManager module, and the related ImageGallery module.

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

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


3 Posts   1156 Views


3 April 2010 at 8:48pm Community Member, 47 Posts

I populate a DropDownField automatically from an array as :

function getCMSFields() {

$fields = new FieldSet( new DropdownField(
    '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;
      return $myDataSet->toDropdownMap("CountryName", "CountryName");      

$fields = new FieldSet( new DropdownField(
    'Choose a country',

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/ on line 584 and defined
IN POST /admin/getitem?ID=11&ajax=1
Line 980 in /home/www/

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

I hope this is not too confusing ...

Thanks in advance for any tip,


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


4 April 2010 at 2:21am 4085 Posts

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);


4 April 2010 at 9:53am Community Member, 47 Posts

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