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.

Data Model Questions

Update fields by date


Reply

3 Posts   409 Views

Avatar
Craftnet

30 December 2012 at 2:01pm Community Member, 58 Posts

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

8 January 2013 at 4:45pm (Last edited: 8 January 2013 4:48pm), Community Member, 528 Posts

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

8 January 2013 at 10:47pm Community Member, 58 Posts

THX For replay but I solved this by Cron