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.

Go to End
Reply


12 Posts   2262 Views

Avatar
Victor

Community Member, 128 Posts

28 March 2009 at 10:43am

I have a an event table SeminarPage with the date saved in format YYYY-MM-DD

If I want to select events of today I simply

$today=date('Y-m-d');
$results = DataObject::get("SeminarPage","`seminardate`='$today'");

===

Now I want to select this week
$thisweek=date('-W');
$results = DataObject::get("SeminarPage"," ....='$thisweek'");

My problem is that seminarweek is not stored in the table and should be calculated as a function of `seminardate`:
seminarweek=date('-W','`seminardate`);

I am not sure how insert this calculation into clause .

Thank you in advance

Victor

Avatar
Nivanka

Community Member, 400 Posts

1 April 2009 at 12:02am

you can get the date from the dataobject which you create and add 7 days to it, and try to work with time epochs it will be the best,

Sometimes you can make use of my diary module. [url]http://silverstripe.org/diary-module/[/url]

Avatar
Victor

Community Member, 128 Posts

1 April 2009 at 12:19am

Thanks. I have the same idea but I am stuck. Probably eventually I will find the script which takes date in 2009-03-31 format and determines the week. Right now we put it manually

BTW week in CalendarDateField selector is shown differently than date('W') returns (probably because in NZ it is counted differently).

However there is another, more deep question. Assume that I want to store in database some function of of other fields: f.e. I have lastname, firstname and fullname which should be "firstname lastname". How to create CustomPage.php which creates an entry form for firstname and lastname but enters fullname automatically (this example is obviously artificial).

Thanks. Victor

Avatar
Ben Gribaudo

Community Member, 181 Posts

1 April 2009 at 2:27am

You might be able to do this in onBeforeWrite().

Your data object could have a field for your "computed" type. You would compute the and set the field's value in onBeforeWrite().

Avatar
Victor

Community Member, 128 Posts

1 April 2009 at 2:35am

Thanks. Should be in this example be

function onBeforeWrite() {
$fullname="$firstname $lastname"
}
{/code]

Victor

Avatar
Ben Gribaudo

Community Member, 181 Posts

1 April 2009 at 2:37am

Off the top of my head, I think it would be like (but I haven't tried it):

function onBeforeWrite() {
$this->fullname = $this->firstname . ' ' . $this->lastname;
parent::onBeforeWrite();
}

Avatar
Victor

Community Member, 128 Posts

1 April 2009 at 2:44am

Thanks! Now I need to find out the calculation needed to find the Week Number from date.

Victor

Avatar
Victor

Community Member, 128 Posts

1 April 2009 at 3:59am

Edited: 01/04/2009 4:48am

In forms

$thisdate=$_POST['seminardate'];
echo strftime("%Y-%V-%u", strtotime($seminardate))


this works fine; but

function onBeforeWerite(){
$this->seminarweek = strftime("%Y-%V-%u", strtotime($this->seminardate));
}


reports javascript error

Go to Top