Jump to content


How can I set a datetime column?

datetime

  • Please log in to reply
9 replies to this topic

#1 kenvogt

kenvogt

    Advanced Member

  • Members
  • PipPipPip
  • 51 posts

Posted 24 May 2012 - 11:45 PM

I have a mysql table with a column called 'updated'. It is of type datetime. I want this column updated automatically any time a row is updated. So I added the following callback and its associated function:
$crud->callback_before_update(array($this,'callback_update'));

...

function callback_update($post_array)
{
  $post_array['updated'] = date('Y-m-d H:i:s');
  return $post_array;
}
I have tried every format I can think of for setting the correct value of 'updated' but nothing is working. What should I be using in place of date('Y-m-d H:i:s')?

One other thing to note, 'updated' is listed in ->columns() but not in ->edit_fields().

#2 kenvogt

kenvogt

    Advanced Member

  • Members
  • PipPipPip
  • 51 posts

Posted 25 May 2012 - 08:08 PM

There are two problems. The first is the format. The correct format is 'd/m/Y H:i:s' for uk-date, 'm/d/Y H:i:s' for us-date, and 'Y-m-d H:i:s' for sql-date.

However I need help with the second problem. Since the 'updated' column is not in the ->edit_fields() list, the value set in the callback is ignored. I do not want this field to be editable by the user however. How do I get around this?

#3 kenvogt

kenvogt

    Advanced Member

  • Members
  • PipPipPip
  • 51 posts

Posted 25 May 2012 - 09:47 PM

Regarding problem #2, I wish there was something like ->hidden_fields(). I suspect that would allow us to update the included fields in a callback like ->fields(), ->add_fields, and ->edit _fields() allow...

#4 kenvogt

kenvogt

    Advanced Member

  • Members
  • PipPipPip
  • 51 posts

Posted 25 May 2012 - 09:49 PM

Actually, I guess we would also need ->add_hidden_fields() and ->edit_hidden_fields().

#5 kenvogt

kenvogt

    Advanced Member

  • Members
  • PipPipPip
  • 51 posts

Posted 25 May 2012 - 10:12 PM

OK, there kinda is already. If I include 'updated' in ->edit_fields() and also use ->change_field_type('updated', 'hidden'), I can now access the field in a callback without it being displayed for edit.

#6 kenvogt

kenvogt

    Advanced Member

  • Members
  • PipPipPip
  • 51 posts

Posted 25 May 2012 - 10:19 PM

Except now it doesn't recognize that the field type for 'updated' used to be datetime so I am back to be uncertain what data to send. BTW, is this a forum or am I just talking to myself here?

#7 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,144 posts
  • LocationLondon

Posted 25 May 2012 - 10:25 PM

I have a mysql table with a column called 'updated'. It is of type datetime. I want this column updated automatically any time a row is updated. So I added the following callback and its associated function:

$crud->callback_before_update(array($this,'callback_update'));

...

function callback_update($post_array)
{
  $post_array['updated'] = date('Y-m-d H:i:s');
  return $post_array;
}
I have tried every format I can think of for setting the correct value of 'updated' but nothing is working. What should I be using in place of date('Y-m-d H:i:s')?

One other thing to note, 'updated' is listed in ->columns() but not in ->edit_fields().


You have to add the invisible type of field if the field is not at your form . So if you simply do something like:

$crud->fields('field1','field2','field4','updated');
$crud->change_field_type('updated','invisible');

it will work just fine...
Posted Image

#8 kenvogt

kenvogt

    Advanced Member

  • Members
  • PipPipPip
  • 51 posts

Posted 25 May 2012 - 10:33 PM

And the answer is to use the sql-date format, since it is not recognizing the hidden field as type datetime or type date, and therefore will not be translating it from the date format specified in application/config/grocery_crud.php.

#9 kenvogt

kenvogt

    Advanced Member

  • Members
  • PipPipPip
  • 51 posts

Posted 25 May 2012 - 10:41 PM

To save anyone from having to read all that back and forth, here is the functioning code. This code updates two columns upon inserting a row ('created' and 'updated') and one column upon updating a row ('updated'):
...

$crud->add_fields('created','updated',{other fields});
$crud->edit_fields('updated',{other fields});

$crud->change_field_type('created','hidden');
$crud->change_field_type('updated','hidden');

$crud->callback_before_insert(array($this,'callback_insert'));
$crud->callback_before_update(array($this,'callback_update'));

...

function callback_insert($post_array)
{
  $post_array['created'] = date('Y-m-d H:i:s');
  $post_array['updated'] = date('Y-m-d H:i:s');
  return $post_array;
}

function callback_update($post_array)
{
  $post_array['updated'] = date('Y-m-d H:i:s');
  return $post_array;
}


#10 kenvogt

kenvogt

    Advanced Member

  • Members
  • PipPipPip
  • 51 posts

Posted 25 May 2012 - 11:50 PM

If you are willing to put up with the datetime fields being set when the form is created rather than when it is submitted, you could do it even simpler without the callbacks:
$crud->add_fields('created','updated',{other fields});
$crud->edit_fields('updated',{other fields});

$crud->change_field_type('created','hidden',date('Y-m-d H:i:s'));
$crud->change_field_type('updated','hidden',date('Y-m-d H:i:s'));






Also tagged with one or more of these keywords: datetime

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users