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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Archive /

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

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

Does widget support getter/setter?


Go to End


5 Posts   1875 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
(deleted)

Community Member, 473 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
(deleted)

Community Member, 473 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.
:(((