Jump to:

3430 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Overloading getters in DataObject not working

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

Page: 1
Go to End
Author Topic: 530 Views
  • Urug
    Avatar
    Community Member
    4 Posts

    Overloading getters in DataObject not working Link to this post

    Calling $this->Property or $this->obj('Property') in DataObject overloaded method getProperty() results in [Notice] Undefined property: DataObject::$Property.

    Interesting fact: Calling $this->Nonexistent (where Nonexistent is any undefined property) doesn't result in any Notice/Error whatsoever.

    Example:

    class News extends DataObject {
    public static $db = Array(
    'Title' => 'Text',
    'Content' => 'HTMLText',
    );

    public function getTitle() {
    Debug::dump($this->Content); //works fine as we are not in getContent() method
    Debug::dump($this->AnythingUndefined); //doesn't cause any trouble, returns empty string
    return $this->Title; //source of the problem
    }
    }

    using SilverStripe 3.0.5

  • simon_w
    Avatar
    Forum Moderator
    471 Posts

    Re: Overloading getters in DataObject not working Link to this post

    That is PHP protecting you from infinite recursion, since $this->Title would end up calling $this->getTitle(), which would access $this->Title and call $this->getTitle() and so in.

    To get the actual value, use $this->getField('Title'); Likewise, in setTitle($value), you'd use $this->setField('Title', $value);

  • Urug
    Avatar
    Community Member
    4 Posts

    Re: Overloading getters in DataObject not working Link to this post

    Thanks for the answer, it truly works, but it has a flaw. getField('Property') returns raw data, which means I can't do this for example:

    public function getDate() {
    return $this->getField('Date')->Day();
    }


    since the getField('Date') doesn't return object Date.

    Nevertheless tutorial at http://doc.silverstripe.org/framework/en/topics/datamodel (section Overloading) is misleading and should be corrected imo.

    530 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.