Jump to:

7935 Posts in 1536 Topics by 943 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » DataObject: Member of the Month

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 299 Views
  • Arno
    Avatar
    Community Member
    2 Posts

    DataObject: Member of the Month Link to this post

    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!

  • zenmonkey
    Avatar
    Community Member
    527 Posts

    Re: DataObject: Member of the Month Link to this post

    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.

    299 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.