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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

onBeforeWrite question


Go to End


14 Posts   11763 Views

Avatar
zim

Community Member, 135 Posts

13 November 2009 at 4:11am

In my CMS I have a page type News_Page.php.

in it I have a checkbox 'IsMainStory' which I check to make the story the headline story at top of holder page - and is thus displayed differnetly.

The problem is that as more and more news pages are added the 'IsMainStory' is still checked on other stories.

I want to check if the 'IsMainStory' is ticked on page I am about to save... and if it is I want to uncheck all other news page stories 'IsMainStory' checkbox.

Does this make sense? Does anyone know the correct way to do this. I believe I should use the 'onBeforeWrite ()' method.

Avatar
juandavidgc

Community Member, 36 Posts

13 November 2009 at 7:41am

Hi Zim!

The correct use of onBeforeWrite is:

http://doc.silverstripe.org/doku.php?id=datamodel#onbeforewrite

You can use it in your News_Page.php, something like this:

class News_Page extends Page {

public static $db = array(
"IsMainStory" => "Boolean"
);

public static $has_one = array(
);

function onBeforeWrite() {
if( $this->IsMainStory )
//IsMainStory is checked
else
//IsMainStory is not checked
parent::onBeforeWrite();

}

}

class News_Page_Controller extends Page_Controller {
public function init() {
parent::init();
}
}

I think this could run fine. Try it.

Good luck!

Avatar
zim

Community Member, 135 Posts

13 November 2009 at 10:45am

sorry this is probably basic question

but how do i actually make IsMainStory=false on all the other records?

do i put it in here

function onBeforeWrite() {
if( $this->IsMainStory )
//IsMainStory is checked
else

IsMainStory=false

parent::onBeforeWrite();

}

I am a bit confused?

Avatar
zim

Community Member, 135 Posts

13 November 2009 at 10:54am

.. in fact it would be in the first bit? am i on the right lines?

function onBeforeWrite() {

if( $this->IsMainStory )

//IsMainStory is checked so uncheck it before checking new one?

IsMainStory=false //is this the right syntax?

else

parent::onBeforeWrite();

}

Avatar
juandavidgc

Community Member, 36 Posts

13 November 2009 at 12:04pm

the right sintax is:

$this->IsMainStory=false

you have to use $this-> before IsMainStory, because IsMainStory is an attribute of the Class News_Page.

Good luck!

Avatar
bummzack

Community Member, 904 Posts

13 November 2009 at 8:22pm

Edited: 13/11/2009 8:23pm

The onBeforeWrite method is only called on the DataObject that is about to be saved. It won't be called on all other pages automatically, so you have to take care of that yourself. Your problem can easily be solved with an SQL query.. here's an example code:

function onBeforeWrite() {
	// call parent first
	parent::onBeforeWrite();
	
	// check if MainStory is true
	if( $this->IsMainStory ){
		// if yes, make sure no other story is main-story
		// we use a raw sql query here to set all IsMainStory to 0
		DB::query('UPDATE ' . $this->ClassName . ' SET IsMainStory=0');
	}
}

I assumed, that you have a table with a the same name as your Class in the DB (NewsPage?). If yes, the provided code should work out of the box. If that's not the case, find out in which table the IsMainStory field is stored and fill in the table name here:

DB::query('UPDATE MyTable SET IsMainStory=0');

Avatar
zim

Community Member, 135 Posts

14 November 2009 at 2:32am

dude this is perfect..

It is setting all IsMainStory to 0.

However It is not setting current IsMainStory to 1.

Would it just be

function onBeforeWrite() {
// call parent first
parent::onBeforeWrite();

// check if MainStory is true
if( $this->IsMainStory ){
// if yes, make sure no other story is main-story
// we use a raw sql query here to set all IsMainStory to 0
DB::query('UPDATE ' . $this->ClassName . ' SET IsMainStory=0');
DB::query('UPDATE ' . $this . ' SET IsMainStory=1);
}
}

Avatar
zim

Community Member, 135 Posts

14 November 2009 at 7:13am

Got it working with this

function onBeforeWrite() {

// check if MainStory is true
if( $this->IsMainStory ){
// if yes, make sure no other story is main-story
// we use a raw sql query here to set all IsMainStory to 0
DB::query('UPDATE ' . $this->ClassName . ' SET IsMainStory=0');

DB::query('UPDATE ' . $this . ' SET IsMainStory=1 WHERE ID=' . $this->ID);

}

// call parent first
parent::onBeforeWrite();

}

Thanks for your help people :)

Go to Top