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.

Archive

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

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

Does widget support getter/setter?


Reply

5 Posts   1502 Views

Avatar
kk

24 June 2008 at 3:33pm Community Member, 9 Posts

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

Avatar
simon_w

24 June 2008 at 4:16pm Forum Moderator, 474 Posts

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

Avatar
kk

24 June 2008 at 5:46pm Community Member, 9 Posts

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);
   }
}
?>

Avatar
simon_w

24 June 2008 at 8:11pm Forum Moderator, 474 Posts

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')

Avatar
kk

24 June 2008 at 8:53pm Community Member, 9 Posts

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.
:(((