Jump to:

3375 Posts in 999 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Week

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

Page: 1 2
Go to End
Author Topic: 1979 Views
  • Victor
    Avatar
    Community Member
    128 Posts

    Week Link to this post

    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

  • Nivanka
    Avatar
    Community Member
    395 Posts

    Re: Week Link to this post

    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. http://silverstripe.org/diary-module/

  • Victor
    Avatar
    Community Member
    128 Posts

    Re: Week Link to this post

    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

  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

    Re: Week Link to this post

    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().

  • Victor
    Avatar
    Community Member
    128 Posts

    Re: Week Link to this post

    Thanks. Should be in this example be

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

    Victor

  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

    Re: Week Link to this post

    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();
    }

  • Victor
    Avatar
    Community Member
    128 Posts

    Re: Week Link to this post

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

    Victor

  • Victor
    Avatar
    Community Member
    128 Posts

    Re: Week Link to this post

    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

    1979 Views
Page: 1 2
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.