Yii Framework Forum: Handling Date(Time)S In Activerecord - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Handling Date(Time)S In Activerecord Best practice for handling dates (unix timestamps)

#1 User is offline   DocSolver 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 12-December 12

Posted 16 November 2013 - 06:35 PM

I'm creating my first project with Yii2, and stumbled upon a design decision about dates.

Many of my database models have date and/or datetime attributes.
I'm trying to stay as close as possible to Yii2's default design choices, so I have converted my DB columns to unsigned ints, which store UNIX timestamps.

However, gii interpreted my timestamps as integers (creating date columns in MySQL doesn't help: this results in string variables).

I could write getter/setter methods for each date or time attribute in each model, to handle the conversion between timestamps and readable dates. However, I'm curious whether there isn't a more refined method.

Can I circumvent writing all those getter/setter methods somehow? For example by indicating that it are dates.

I have noticed that the validator uses PHP's new Date/Time classes. Would it make sense to have my model return these attributes as such objects? Will this somehow help me with using the dates/times in queries or to parse them in AR forms using the JUI date picker?

I'm always impressed with the brevity and elegance of the framework's code. Writing a lot of similar getters/setters in my code seems to violate the elegance of the framework...

Thanks!
0

#2 User is offline   DocSolver 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 12-December 12

Posted 16 November 2013 - 08:38 PM

I just noticed that the standard 'view.php' generated by Gii does interpret my timestamps as dates (it is showing full dates instead of timestamp integers).

Where is this behavior determinded? How can I combine it with JUI date pickers in my form?

thanks in advance!
0

#3 User is offline   DocSolver 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 12-December 12

Posted 16 November 2013 - 09:24 PM

I think a code example of how to handle dates, all the way from database table (column data type) to JUI in form, and back (to save the dates into updated timestamps) would be incredibly useful.

This is functionality that most apps will need. By setting a best practice now, not everyone building an app will need to reinvent the wheel... Just my 2 cents!
0

#4 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,700
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 16 November 2013 - 09:53 PM

Not a best practice, probably, but I do it this way:

Step 1. Define getter and setter in your model.

public function getCreated()
{
    return $this->create_time ? date('d-m-Y', $this->create_time) : null;
}


Step 2. Use virtual attribute ('created' in the example above) for datepicker.

Dont' forget about validation rules for your new attribute.
God is real unless declared as integer
0

#5 User is offline   DocSolver 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 12-December 12

Posted 17 November 2013 - 03:55 PM

@Orey: that's also what I was thinking.
However, since the standard view generated by Gii is able to convert my Unix timestamp to a readable format automatically, I was hoping for the framework to offer some kind of built-in solution.

After all, the whole reason why I use a framework is to avoid having to think about "trivial" issues like how to store and convert dates....
0

#6 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,700
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 17 November 2013 - 04:09 PM

1. I don't use Gii at all, sorry. Cmd+C / Cmd+V do all the stuff.

2. As for me, the framework must be as clean as possible, without extra magic. That's why I don't use RoR: it's too magic :)

3. And yes, I hate CGridViews and all the magic widgets like bootstrap extensions. They do things that can be done by a couple of lines of code, but bring in a lot of trouble for those who use them (this forum is full of posts like "I cannot do this in that widget, plz help").

Storing and retrieving timestamps can be done exactly in two lines of code (one for getter and one for setter), so no, I don't think it's a good idea to bring this in the core.
God is real unless declared as integer
0

#7 User is offline   DocSolver 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 12-December 12

Posted 18 November 2013 - 05:07 AM

Alright, thanks for the feedback. I'll go with getters and setters!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users