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.

Customising the CMS /

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

how to connect two values ?


Go to End


9 Posts   2916 Views

Avatar
snaip

Community Member, 181 Posts

15 May 2009 at 12:57am

hi

i have two dropdownfields


static $db = array(
'calendar_month' => "Enum('01,02,03,04,05,06,07,08,09,10,11,12','01')",
'calendar_year => "Enum('2009,2010,2011','2009')"
'calendar' => 'text'
);

static $has_one = array(
);

function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.Content.Main", new DropdownField('calendar_month','Select month',singleton('PlOpiniePage')->dbObject('calendar_month')->enumValues()),Content);
$fields->addFieldToTab("Root.Content.Main", new DropdownField('calendar_year','Select year',singleton('PlOpiniePage')->dbObject('calendar_year')->enumValues()),Content);
return $fields;
}

how to connect these two values to one and put it into 'calendar' field ?

01 + 2009 = 012009

Avatar
UncleCheese

Forum Moderator, 4102 Posts

15 May 2009 at 6:12am

A few things come to mind. You could try CompositeDateField. That may get you closer to what you're trying to do. I'm also wondering why you need to store the concatonated string as a database field in the first place. Why not just render it with a template function? Worst case, use onAfterWrite() to write the field manually using the post data.

Avatar
snaip

Community Member, 181 Posts

16 May 2009 at 12:11am

hmmm

i need 'calendar_month' field to print the names of the months in the different languages like this:

<% if calendar_month == 1 %>
january
<% else if calendar_month == 2 %>
february
<% else if calendar_month == 3 %>

.....

and the same in polish, german, spain language

i need 'calendar' field with values 012009, 022009, 032009 .... to compare it in the next/prev link

 public function nextPager() {
  $where = "ParentID = ($this->ParentID + 0) AND Calendar > ($Calendar->Sort + 0 )";
  $pages = DataObject::get("SiteTree", $where, "Calendar", "", 1);
  if($pages) {
   foreach($pages as $page) {
    return $page;
   }
  }
 }

i cant compere these format YYYY-MM-DD which are generating by CompositeDateField

Avatar
snaip

Community Member, 181 Posts

16 May 2009 at 1:32am

thx :)
done it


   public function onAfterWrite() {
      $cp = DataObject::get_by_id('PlOpiniePage',$this->ID);
      $cp->Calendar = $this->Calendar_moth . $this->Calendar_year;
      $cp->write();
      return parent::onAfterWrite();
   }

Avatar
snaip

Community Member, 181 Posts

18 May 2009 at 2:37am

Edited: 18/05/2009 3:16am

ehhhh
now when i click GO button to create new page, the page is creating but CMS doesn't refresh automaticly
i must click F5 to refresh browser

and the same problem is with Save & Publish button
Save & Publish thinking is never ending ...

what is wrog with this onAfterWrite()? ? ?

Avatar
UncleCheese

Forum Moderator, 4102 Posts

18 May 2009 at 3:57am

Edited: 18/05/2009 3:59am

It seems to me you're working way too hard, here. First of all, if you need the months in different languages, just use

strftime('%B', $timestamp);

It's built into PHP.

Second, I have no idea what's going on here:

ublic function nextPager() { 
$where = "ParentID = ($this->ParentID + 0) AND Calendar > ($Calendar->Sort + 0 )"; 
$pages = DataObject::get("SiteTree", $where, "Calendar", "", 1); 
if($pages) { 
foreach($pages as $page) { 
return $page; 
} 
} 
}

Why + 0? And why are you relying on consecutive increments of ParentID? What happens if you delete one? Will this function break?

Why are you using DataObject::get() when you have a limit of 1? Why not DataObject::get_one("SiteTree", $where);

And why are you going into a loop only to return a value in the first iteration? What you have there is the same as:

return $pages->First();

Have you seen the EventCalendar module? You may be reinventing the wheel, here.

Avatar
snaip

Community Member, 181 Posts

18 May 2009 at 9:12am

don't ask me why
here is the tutorial of this prev/next function
http://doc.silverstripe.com/doku.php?id=recipes:previousornext&s=previouspager

Avatar
UncleCheese

Forum Moderator, 4102 Posts

18 May 2009 at 9:26am

I cannot for the life of me understand why someone would do it that way and I question why it's in the Wiki.

If I understand correctly, you're trying to step through your pages in order of their Calendar value. I think this should work fine for you:

public function nextPager() { 
return DataObject::get_one("SiteTree", "ParentID = $this->ParentID AND Calendar > $this->Calendar", true, "Calendar ASC"); 
}

public function nextPager() { 
return DataObject::get_one("SiteTree", "ParentID = $this->ParentID AND Calendar < $this->Calendar", true, "Calendar DESC"); 
}

Go to Top