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.

Data Model Questions /

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

Update fields by date


Go to End


3 Posts   1258 Views

Avatar
Craftnet

Community Member, 58 Posts

30 December 2012 at 2:01pm

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

Avatar
zenmonkey

Community Member, 545 Posts

8 January 2013 at 4:45pm

Edited: 08/01/2013 4:48pm

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')

Avatar
Craftnet

Community Member, 58 Posts

8 January 2013 at 10:47pm

THX For replay but I solved this by Cron