⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

How can I set a datetime column?



kenvogt
  • profile picture
  • Member

Posted 24 May 2012 - 23: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().

kenvogt
  • profile picture
  • Member

Posted 25 May 2012 - 20: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?

kenvogt
  • profile picture
  • Member

Posted 25 May 2012 - 21: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...

kenvogt
  • profile picture
  • Member

Posted 25 May 2012 - 21:49 PM

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

kenvogt
  • profile picture
  • Member

Posted 25 May 2012 - 22: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.

kenvogt
  • profile picture
  • Member

Posted 25 May 2012 - 22:19 PM

[i]Except[/i] 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?

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 25 May 2012 - 22:25 PM

[quote name='kenvogt' timestamp='1337903138' post='1956']
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().
[/quote]

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...

kenvogt
  • profile picture
  • Member

Posted 25 May 2012 - 22:33 PM

[i]And[/i] 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.

kenvogt
  • profile picture
  • Member

Posted 25 May 2012 - 22: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;
}

kenvogt
  • profile picture
  • Member

Posted 25 May 2012 - 23: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'));