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.

Customising the CMS /

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

DataObject -> Unable to Get Field Value?


Go to End


3 Posts   2927 Views

Avatar
Garrett

Community Member, 245 Posts

8 May 2012 at 7:31am

Edited: 08/05/2012 7:32am

Hi,

Experiencing a weirdsie on something I think I must have done a thousand times. I have $db field called "SectorName" in a DataObject which is populated in every case but inside the class $this->SectorName is returning empty. Code:

class Sector extends DataObject {

	public static $db = array(
		"SectorName" => "Text",
		"SectorDescription" => "HTMLText",
		"ClientsToShow" => "Text"
	);

	public static $has_one = array(
		"SectorHolder" => "SectorHolder"
	);

	public static $field_names = array(
		"SectorName" => "Name",
		"SectorDescription" => "Description"
	);
	
	static $can_be_root = false;
	
	static $default_parent = 'SectorHolder';
	
	public function getCMSFields_forPopup() {

		$fields = new FieldSet();

		$fields->push(new TextField("SectorName", "Name"));
		$fields->push(new SimpleTinyMCEField("SectorDescription", "Description"));
		$fields->push(new ListboxField("ClientsToShow", "Clients to Show in ".$this->SectorName, DataObject::get("Client","Sectors LIKE '%".$this->SectorName."%'", "Title ASC")->toDropdownMap('ID','Title','(Select one or more options)',true), "", "5", true));

		return $fields;

	}

}

Why? I am INSIDE the dataobject, why would this not have a value?

Thanks,
Garrett

Avatar
swaiba

Forum Moderator, 1899 Posts

16 May 2012 at 8:26pm

getCMSFields_forPopup

Are you sure that is called on an object populated with data? I'd suggest that an abstact object is created, that method called in order to get the fieldset. That means you are no in the object you think you are. Why not use the getCMSFields method to manipulate the fields? And while you are doing that you needn't create the fields as it will be scafolded for you... you're not using DOM are you?

Avatar
Garrett

Community Member, 245 Posts

19 June 2012 at 6:31am

I am not sure what you mean by my not using DOM. But yes, I am sure it's populated. The Sector class has a $has_one relationship with a SectorHolder, and the Client has a field called Sectors which ends up being a comma-separated list of Sectors associate with it. What I am trying to do is to only show the Clients one of whose sector we are in. However, again, SectorName is always empty here. Tried making it into a function as well ($this->SectorName()) which gets it from the ID on a get_by_id but that doesn't work either. It appears that you are deifnitely right, that i am not in inside this dataobject, but.... in the modal popup when editing a Sector, why does the SectorName appear in the SectorName field, but yet I can't access it when populating the client field? This makes no sense to me. I am attaching all three fields involved in hopes that you may be able to assist me further.

Thanks again,
Garrett

Attached Files