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.

E-Commerce Modules /

Discuss about the various e-commerce modules available:
Ecommerce, SS Shop, SilverCart and SwipeStripe
Alternatively, have a look the shared mailinglist.

Moderators: martimiz, Nicolaas, Sean, Ed, frankmullenger, biapar, Willr, Ingo, Jedateach, swaiba

Call to a member function toDropDownMap() on a non-object in ... Product.php


Go to End


3 Posts   3650 Views

Avatar
giroro

Community Member, 2 Posts

19 June 2009 at 4:56pm

Dear All,

I've tried google around but i've found no solution to creating a dropdown list with a DataObject::get()

The background is that i'm trying to set up a store to show which district(area) is my product is located. This is what i've done.

<?php

class District extends DataObject {

static $db = array(
'Name' => 'Varchar(50)',
);

static $has_one = array(
'StateID' => 'State'
);

}

?>

also a State DataObject

<?php

class State extends DataObject {

static $db = array(
'Name' => 'Varchar(50)'
);

}
?>

Then i want it shows up as a pull down menu in my Product, so i did this in Product.php's getCMSFields()

function getCMSFields() {
$fields = parent::getCMSFields();

...
//Database request for the object
$myDataSet = DataObject::get("District");

# if($myDataSet){
// This returns an array of ID => Title
$map = $myDataSet->toDropDownMap();

$fields->addFieldToTab("Root.Content.Main", new ListboxField(
'District',
'District',
$map,
$map[0])
);
# }
...
return $fields;
}

But DataObject::get("District"); gives me null, it seems that Product.php cannot access the District object.

It seems my O-O concepts are really bad, can anyone give me a hand?

Thanks in advance!

Avatar
SilverRay

Community Member, 167 Posts

19 June 2009 at 5:30pm

Well, probably you forgot to set up the other end of the relation in your Product.php, like so for instance (if you use a has_many):

	public static $has_many = array(
		'District' => 'District'
	);

and in toDropDownMap you might want to add:

$map = $myDataSet->toDropDownMap('ID', 'Name');

See also:

http://doc.silverstripe.com/doku.php?id=tutorial:5-dataobject-relationship-management

and

http://doc.silverstripe.com/doku.php?id=optionsetfield&s=todropdownmap

Helps?

Avatar
giroro

Community Member, 2 Posts

19 June 2009 at 6:08pm

Thanks for the prompt reply.

Well... i did actually declare

static $has_one = array(
'Image' => 'Product_Image',
'District' => 'District'
);

But Product stilll can't access to DataObject::get("District").

I'll go thru the http://doc.silverstripe.com/doku.php?id=tutorial:5-dataobject-relationship-management tutorial. I'll post my results if i get it right.