21491 Posts in 5783 Topics by 2621 members
| Go to End | Next > | |
| Author | Topic: | 4028 Views |
-
onBeforeWrite question

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.
-
Re: onBeforeWrite question

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!
-
Re: onBeforeWrite question

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
elseIsMainStory=false
parent::onBeforeWrite();
}
I am a bit confused?
-
Re: onBeforeWrite question

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();
}
-
Re: onBeforeWrite question

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!
-
Re: onBeforeWrite question

13 November 2009 at 8:22pm Last edited: 13 November 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');
-
Re: onBeforeWrite question

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);
}
} -
Re: onBeforeWrite question

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
| 4028 Views | ||
| Go to Top | Next > |


