jonathangeiger / kohana-jelly

See the link below for the most up-to-date code
https://github.com/creatoro/jelly
MIT License
146 stars 34 forks source link

Timestamp field issue #107

Closed paulchubatyy closed 14 years ago

paulchubatyy commented 14 years ago

I have the following four fields in my model. The declaration is pretty straightforward.

            'last_answered_at' => new Field_Timestamp,
            'resolved_at' => new Field_Timestamp,
            'created_at' => new Field_Timestamp(array(
                'auto_now_create' => TRUE,
            )),
            'updated_at' => new Field_Timestamp(array(
                'auto_now_update' => TRUE,
            )),

But when creating (saving for the first time) the model all the fields are stored with the current time except of created_at.

Here is the sql I used for the field declaration:

  `last_answered_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `resolved_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',

cheers!

dewos commented 14 years ago

Emmm... so... what's the problem? :)

paulchubatyy commented 14 years ago

The fields are acting vice versa. If "auto_now_create" is set to true the field doesn't get the current time as default value

dewos commented 14 years ago

Ok, maybe because Jelly uses UNIX timestamps (INT), not date, datetime, or other such string-type fields.

paulchubatyy commented 14 years ago

"timestamp" is saved as int in the database as far as I know.

banks commented 14 years ago

"timestamp" is saved as int in the database as far as I know.

Timestamp is stored as a mysql timestamp (which is int based but is not treated just like an int by the query).

Jelly timestamp fields are designed to be used with straight Integer fields where the actual value in the queries is specified as a timestamp throughout. There are many points of view about whether this is a good solution or not and it will vary based on your situation. That is the convention though and is how Jelly is designed.

If you want to use mysql date or timestamp fields, you will need to write your own field classes that input and output the data as mysql expects.

I'll close this as the wrong data type is almost certainly the problem here.

jonathangeiger commented 14 years ago

Jelly timestamp fields are designed to be used with straight Integer fields where the actual value in the queries is specified as a timestamp throughout

This isn't entirely true. When interacting with the model, timestamp data always comes back as UNIX timestamps (integers), however they can be configured so that before saving they are converted into a format the database recognizes.

The fields should be set up as this:

'foo' => new Field_Timestamp(array(
            'auto_now_update' => TRUE,
            'format' => 'Y-m-d H:i:s'
        ));

'format' specifies the string passed to PHP's date function to format the time.

paulchubatyy commented 14 years ago

Thanks Jonathan, I've got your point and the field is working as intended with format string.