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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

DataObject: Member of the Month


Go to End


2 Posts   1006 Views

Avatar
Arno

Community Member, 2 Posts

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!

Avatar
zenmonkey

Community Member, 545 Posts

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.