cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[NFR] Datetime column type

Open SliceOfLife opened this issue 13 years ago • 21 comments

What about adding "datetime" column type for Model? By default the "@var \Datetime" converted to string.

SliceOfLife avatar Apr 05 '13 14:04 SliceOfLife

@SliceOfLife I am not sure I understand what you mean. Could you please explain with an example what you are trying to do ?

niden avatar Apr 05 '13 16:04 niden

For example, I have a Mysql DB and table with datetime column. I want to write the Model class with \DateTime field:

class SomeModel extends \Phalcon\Mvc\Model 
{
    /**
     * @Column(type="datetime", nullable=false)
     *
     * @var \DateTime
     */
    public $datetime_field;

    public function initialize()
    {
        $this->datetime_field = new \DateTime();
    }
}

But I can't use the "datetime" column type, it's very sad. This code throws error like "Object of class DateTime�����w could not be converted to string".

SliceOfLife avatar Apr 05 '13 17:04 SliceOfLife

Hmmm for some reason my comment disappeared.

Either way.

This is not a blocker and the desired functionality can be achieved with a minor change in the code. We can add it as a NFR but it will take a low priority one and we also need to test the impact that this will have on the framework as far as performance is concerned.

You can achieve what you need with:

$now = new \DateTime();
$this->datetime_field = $now->format('Y-m-d H:i:s');

or without using datetime

$this->datetime_field = date('Y-m-d H:i:s');

niden avatar Apr 05 '13 18:04 niden

It will be a cool feature. By the way, I use an integer timestamp and Mysql INT(11) type for now.

SliceOfLife avatar Apr 05 '13 19:04 SliceOfLife

I follow the same practice too (using integers). Much faster to search/index, you can perform easy math operations with them (to find differences etc.) and for me at least it is a lot more logical. You also escape the different formatting conventions i.e. m/d/y, y.m.d etc.

niden avatar Apr 05 '13 19:04 niden

@dalu I haven't personally used a 32bit machine in a few years and are seeing them disappear. However, the easy solution on that would be again numeric fields. You will just need to have two per date (if you want to store hours/minutes/seconds)

For the date I would store this:

20130414 (which is 2013-04-14)

and for the time the same thing

175432 (which is 17:54:32)

This way you have no issues with 32 bit machines. :)

Having said that, with the way technology is going, I doubt we will have any 32bit machines by that year.

niden avatar Apr 14 '13 15:04 niden

Closing this as we will not be implementing automatic Datetime fields in Phalcon in the very near future. Perhaps in 2.0+

niden avatar Apr 16 '13 15:04 niden

+1 for this!

lfbittencourt avatar Dec 05 '14 20:12 lfbittencourt

+1

tmihalik avatar Jan 14 '15 08:01 tmihalik

+1

AndreBaumeier avatar Mar 26 '15 14:03 AndreBaumeier

+1 I would welcome it if Phalcon translates the DateTime object to the format expected by the database backend. With this layer of abstraction, code should become more portable.

loopsframework avatar Jun 09 '15 02:06 loopsframework

:+1:

f3l1x avatar Oct 06 '15 12:10 f3l1x

@niden

Closing this as we will not be implementing automatic Datetime fields in Phalcon in the very near future. Perhaps in 2.0+

Now is 2.0+ :)

sergeyklay avatar Oct 06 '15 13:10 sergeyklay

I vote for this feature implemented about now.

MLukman avatar Nov 14 '15 11:11 MLukman

+1

StudioMaX avatar Mar 10 '16 18:03 StudioMaX

+1

reloaded avatar Mar 05 '17 02:03 reloaded

+1

efimBistrov avatar Apr 25 '17 10:04 efimBistrov

Hi,

I've found this thread and I would like to add the following:

  • Regarding the performance impact, there is already a "castOnHydrate" option to disable if you don't need it or if you want performance (by default it comes disabled).
  • For this implementation there should be methods on the Phalcon\Db\Dialect to establish the default date format returned by the engine for each date type (i.e. date or timestamp) and methods to transform the result from those to a \DateTime object. Note: I think here is the actual casting.

My personal opinion about this, is that an ORM should abstract you on the date format the database engine is using and should use native language date manipulations. This adds transparency when you want or need to change a database engine.

Regards, Federico.

kornerita avatar Aug 16 '17 19:08 kornerita

Thank you for contributing to this issue. As it has been 90 days since the last activity, we are automatically closing the issue. This is often because the request was already solved in some way and it just wasn't updated or it's no longer applicable. If that's not the case, please feel free to either reopen this issue or open a new one. We will be more than happy to look at it again! You can read more here: https://blog.phalconphp.com/post/github-closing-old-issues

stale[bot] avatar Apr 16 '18 17:04 stale[bot]

This is still relevant.

StudioMaX avatar Apr 16 '18 18:04 StudioMaX