7913 Posts in 1355 Topics by 930 members
DataObjectManager Module
SilverStripe Forums » DataObjectManager Module » DataObject: Member of the Month
Discuss the DataObjectManager module, and the related ImageGallery module.
Moderators: martimiz, UncleCheese, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 185 Views |
-
DataObject: Member of the Month

28 March 2012 at 2:29am
Hello,
Firstly, I am using SilverStripe for a couple months now and I must say, I really enjoy workign with it. Unfortuantly I am having a problem.
A customer would like to have a Member of the Month out of his database of members and this on the frontpage. Currently I am calling the Member of the Month the following way.
//Get Trojan of the Month
public function getTrojanoftheMonth(){
return DataObject::get_one("Leden", "", "", "", "1");
}Is there a way where I can tell the dataobject to get a new member after one month? I hope any of you amazing people could help me, I have been able to learn alot through this forum from just reading!
-
Re: DataObject: Member of the Month

12 April 2012 at 2:41am
At its simplest you need to add a CurrentTrojan Varchar to Leden DataObject to store the current month the do something like this
function getTrojanoftheMonth(){
$currentTrojan = DataObject::get_one("Leden", "CurrentTrojan=".date("M"));
if($currentTrojan) {
return $currentTrojan;
}else{
$oldTrojan = DataObject::get_one("Leden", "CurrentTrojan IS NOT NULL ");
$newTrojan = DataObject::get_one("Leden", "CurrentTrojan IS NULL ",false, 'RAND()');
if($oldTrojan && $newTrojan) {
$oldTrojan->CurrentTrojan = NULL;
$oldTrojan->write();
$newTrojan->CurrentTrojan = date("M");
$newTrojan->write();
return $newTrojan;
} else {
return false;
}
}
}
Basically you're looking at Leden for anyone whose CurrentTrojan is the same month as the current month, if there isn't one with the current month to looks for the old one and a new random one. On the old one it removes the month marker and on the new one it sets the month to the current month and returns it. I didn't include a case for the first time you run it (It'll fail because it won't find an oldTrojan) you can either add the fallback case or just manually set the CurrentTrojan of a Leden before call the function.
| 185 Views | ||
|
Page:
1
|
Go to Top |


