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   1517 Views

Avatar
kk

Community Member, 9 Posts

24 June 2008 at 3:33pm

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

Forum Moderator, 474 Posts

24 June 2008 at 4:16pm

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

Avatar
kk

Community Member, 9 Posts

24 June 2008 at 5:46pm

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

Forum Moderator, 474 Posts

24 June 2008 at 8:11pm

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

Community Member, 9 Posts

24 June 2008 at 8:53pm

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