Jump to:

5541 Posts in 1738 Topics by 1224 members

Customising the CMS

SilverStripe Forums » Customising the CMS » DataObject -> Unable to Get Field Value?

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

Page: 1
Go to End
Author Topic: 1023 Views
  • Garrett
    Avatar
    Community Member
    245 Posts

    DataObject -> Unable to Get Field Value? Link to this post

    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

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: DataObject -> Unable to Get Field Value? Link to this post

    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?

  • Garrett
    Avatar
    Community Member
    245 Posts

    Re: DataObject -> Unable to Get Field Value? Link to this post

    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
    1023 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.