Jump to:

3070 Posts in 869 Topics by 651 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Update fields by date

Moderators: martimiz, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 208 Views
  • Craftnet
    Avatar
    Community Member
    46 Posts

    Update fields by date Link to this post

    Hi, I have problem.
    I try to write function to change fields in DB when date exceeds specific date

    I have DataObject "Obiekt"

    <?php

    class Obiekt extends DataObject {

    static $db = array(
    'NrObiektu' => 'Int',
    'Title' => 'Varchar(255)',
    ...
    'Created' => 'Date',
    'Waznosc' => 'Date', //for when to display the object
    "Status" => "Enum('Oplacony, Nieoplacony')",
    );

    static $indexes = array(
    "URLSegment" => true
    );

    public function getCMSFields() {
    $fields = parent::getCMSFields();
    ...
    return $fields;
    }

    function onBeforeWrite() {
    ...
    }

    }

    I don't know how write this and where placed this (maybe to function or init() or other place?)
    Maybe by DB:: ?

    Now I have to check date like this

    $now = date('Y-m-d H:i:s');
    $where = "Waznosc < '$now'";

    I want to when $where is true change field Status from Oplacony to Nieoplacony

    Sorry for my bad English

  • zenmonkey
    Avatar
    Community Member
    483 Posts

    Re: Update fields by date Link to this post

    You need to convert the Database value into a unix timestamp so your conditional statement would be something like

    strtotime($this->Waznosc) < time()

    time() returns the current time as Unix Time Stamp so you don't need to worry about formatting it like with date()

    As an aside strtotime() is also great for calculating things like strtotime('next Sunday') or strtotime('Yesterday')

  • Craftnet
    Avatar
    Community Member
    46 Posts

    Re: Update fields by date Link to this post

    THX For replay but I solved this by Cron

    208 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.