Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Does widget support getter/setter?

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 1488 Views
  • kk
    Avatar
    Community Member
    9 Posts

    Does widget support getter/setter? Link to this post

    like this

    http://doc.silverstripe.com/doku.php?id=datamodel#overloading

    “Getters” and “Setters” are functions that help us save fields to our data objects

  • simon_w
    Avatar
    Forum Moderator
    471 Posts

    Re: Does widget support getter/setter? Link to this post

    Yes. They support everything that you can have from a normal page.

  • kk
    Avatar
    Community Member
    9 Posts

    Re: Does widget support getter/setter? Link to this post

    but seems it does not work

    <?php
    class TestWidget extends Widget {
       static $db = array(
             "Myvalue" => "Text"
          );

       static $title = "Test widget";
       static $cmsTitle = "Test widget";
       static $description = "Test Test Test Test";

       function getCMSFields() {
          return new FieldSet(
             new TextField("Myvalue" , "Myvalue" )
          );
       }
       
       function getMyvalue(){
          if($this->Myvalue){
             return $this->Myvalue;
          }else{
             return "default value";
          }
       }
       
       function setMyvalue($value){
          $this->setField("Myvalue" , "hoho".$value);
       }
    }
    ?>

  • simon_w
    Avatar
    Forum Moderator
    471 Posts

    Re: Does widget support getter/setter? Link to this post

    This is because when you're calling $this->Myvalue in getMyvalue, SilverStripe then goes and calls getMyvalue, which causes an infinite loop. You need to use $this->getField('Myvalue')

  • kk
    Avatar
    Community Member
    9 Posts

    Re: Does widget support getter/setter? Link to this post

    i changed as following

    function getMyvalue(){
          //return $this->getField("Myvalue");
          return "myvalue";
       }

    it does not work even i return a string

    and the
    function setMyvalue($val){
    $this->setField("Myvalue" , "prefix".$val."suffix");
    }

    it can not work too.
    ((

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