Next: Drupal 7 – Create a Datetime field in hook_schema() @ 1
If your custom module requires an additional table in the Drupal database, you have to create the .install file and define the schema inside it. In Drupal 7, the datetime field is no longer support by the Drupal Schema API. If you want to create a datetime field, you have to use the mysql_type or pgsql_type. Here is an example.
<?php function <module>_schema() { $schema = array(); $schema['<table name>'] = array( 'description' => t('<table description>'), 'fields' => array( 'id' => array( 'description' => t('The primary identifier.'), 'type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE ), 'created_at' => array( 'description' => t('The creation date.'), 'mysql_type' => 'DATETIME', 'not null' => TRUE ), ), 'primary key' => array('id'), ); return $schema; }
In the above case, you can save the value of the created_at value by
$id = db_insert('<table name>') ->fields(array( 'created_at' => format_date(time(), 'custom', 'Y-m-d H:i:s') ) ) ->execute();
Done =)
Reference:
thanks.
LikeLike
You are welcome. =)
LikeLike