Jump to:

3429 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Update fields by date

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

Page: 1
Go to End
Author Topic: 385 Views
  • Craftnet
    Avatar
    Community Member
    58 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
    527 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
    58 Posts

    Re: Update fields by date Link to this post

    THX For replay but I solved this by Cron

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