Create database table using Drupal hook_schema() - php

For the last few hours I have been trying to create database table in my custom module with no luck!! The documentation says that you only need to implement hook_schema in Drupal 7 and the Drupal takes care of creating the table during module installation. I just cant figure out why the table is not created. I tried installing the module through Drupal admin as well as pasting in the module folder but it still doesnt work.
Here is my function inside .install file:
/**
* #return array an array containing table's field definitions
* to be created by drupal schema api
*/
function inquiry_form_schema() {
$schema['inquiry_form'] = array(
'description' => 'Table to record inquiries submitted by staff',
'fields' => array(
'ID' => array(
'description' => 'The primary key of the table',
'type' => 'serial',
'size' => 'big',
'not null' => TRUE,
),
'sender' => array(
'description' => 'The person who submits the inquiry',
'type' => 'varchar',
'length' => '100',
'not null' => TRUE,
),
'subject' => array(
'description' => 'Title of the inquiry being submitted',
'type' => 'char',
'length' => '100',
'not null' => TRUE,
),
'department' => array(
'description' => 'The department that the inquiry is being referenced to',
'type' => 'varchar',
'length' => '100',
'not null' => TRUE,
),
'description' => array(
'description' => 'Detailed explanation of the issue the sender is experiencing',
'type' => 'text',
'not null' => TRUE,
),
// 'date' => array(
// 'description' => 'Date of the inquiry being submitted',
// 'mysql_type' => 'datetime',
// 'not null' => TRUE,
// ),
),
'primary key' => array('ID'),
);
return $schema;
}

I'm assuming your files are:
- inquiry_form.info
- inquiry_form.module
- inquiry_form.install
Is this the case?
Make sure the length is not quoted.
Should be 100 and not '100'

Related

drupal_write_record in drupal 6 returning false

When I run drupal_write_record, it always comes back false. I've debugged into the function and see that drupal_get_schema function, found inside drupal_write_record function, returns false. Further more, if I debug into drupal_get_schema function and look at
$schema = $cached->data;
My new table is not included in the $schema array. I've tried flushing the cache, reinstalling the module, and running update.php but nothing helps. In the event it helps, I've also included the .install file I wrote that adds the new table to the database:
<?php
// $Id: my_module.install,v 1.00.0.1 2012/10/21 00:23:32 Exp $
/**
* Implementation of hook_schema().
*/
function my_module_schema() {
$schema = array();
$schema['my_module_pending_inserts'] = array(
'fields' => array(
'id' => array(
'description' => 'primary key',
'type' => 'serial',
'size' => 'tiny',
'not null' => TRUE,
),
'json_array_data' => array(
'type' => 'text',
'not null' => TRUE,
),
'successfully_run' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'date_created' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'date_updated' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
)
),
'primary key' => array('id'),
);
return $schema;
}
function jira_reporting_install() {
// Create tables.
drupal_install_schema('my_module_pending_inserts');
}
function my_module_uninstall() {
// Remove tables.
drupal_uninstall_schema('my_module_pending_inserts');
// Delete variables
variable_del('my_module_variable1');
variable_del('my_module_variable2');
variable_del('my_module_variable3');
}
function my_module_update_2() {
$up = array();
$tbl = array(
'fields' => array(
'id' => array(
'type' => 'serial', 'not null' => TRUE
),
'json_array_data' => array(
'type' => 'varchar',
'length' => 250,
'not null' => True
),
'successfully_run' => array(
'type' => 'int'
),
'date_created' => array(
'type' => 'int'
),
'date_updated' => array(
'type' => 'int'
),
),
'primary key' => array('id'),
);
db_create_table(&$up, 'my_module_pending_inserts', $tbl);
return $up;
}
never mind, it was a typo in my _schema name

How do I return a custom 'isEmpty' error message from Zend\Validator\Identical in Zend Framework 2? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
zendframework 2 inputfilter customize default error message
I'm trying to use Zend\InputFilter\InputFilter to validate the input from a registration form. I have the code below that:
Validates the email address in the 'email' field; then
Checks the value in 'email_confirm' matches the one in 'email'.
This works in in all instances other than when the user leaves both fields blank. In that instance the validator for 'email_confirm' returns the error Array ( [isEmpty] => Value is required and can't be empty ).
How do I customise this error message? I cannot set it using:
'messages' => array(
'isEmpty' => 'Message Here'
),
because that throws an exception saying (quite rightly) that Zend\Validator\Identical does not have a message template for 'isEmpty'. And it's not picking up the messages I've previously set for the 'email' field, otherwise it would be returning Array ( [isEmpty] => Please enter your email address ).
$this->add($inputFactory->createInput(array(
'name' => 'email',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' =>'NotEmpty',
'options' => array(
'messages' => array(
'isEmpty' => 'Please enter your email address'
),
),
),
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'max' => 200,
'messages' => array(
'stringLengthTooLong' => "Email addresses cannot be more than 200 characters"
),
),
),
array(
'name' =>'EmailAddress',
'options' => array(
'useMxCheck' => true,
),
),
),
)));
$this->add($inputFactory->createInput(array(
'name' => 'email_confirm',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'Identical',
'options' => array(
'token' => 'email',
'messages' => array(
'notSame' => "Your email addresses do not match, please try again",
),
),
),
),
)));
Thanks,
Neil
You have to use Zend\Validator\NotEmpty::IS_EMPTY constant this way:
array(
'name' =>'NotEmpty',
'options' => array(
'messages' => array(
\Zend\Validator\NotEmpty::IS_EMPTY => 'Please enter your email address'
),
),
),
You can check out the API for further info.
EDIT
Diving into Zend\Validator\NotEmpty source code I see that IS_EMPTY constant has 'isEmpty' value, so it won't fix your problem. Anyway, you should use these constants instead of direct values in order to avoid possible updating issues.

drupal 7 custom schema error datetime

I have the following schema (generated from an existing table with the schema module (7.x-1.0-beta3) in a custom module.
function myproject_entities_schema() {
// ---------------------------------------------------------------------------------
// MESSAGE
// ---------------------------------------------------------------------------------
$schema['myproject_entity_message'] = array(
'description' => 'The base table for myproject message instances',
'fields' => array(
'id' => array(
'description' => 'The primary identifier for a message instance',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'weekendday' => array(
'description' => 'Whether this message gets send on a weekendday(1) or on a workday(0)',
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
),
'day' => array(
'description' => 'Numbered day of campaign schedule',
'type' => 'int',
'not null' => TRUE,
),
'content' => array(
'description' => 'Message content',
'type' => 'text',
'not null' => TRUE,
),
'time_to_send' => array(
'description' => 'When the message should arrive at the user',
'type' => 'datetime',
'not null' => TRUE,
),
'campaign_fk' => array(
'description' => 'the foreign key of the campaign this message belongs to',
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
),
'user_id' => array(
'description' => 'The user id this message is generated for',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'pool_id' => array(
'description' => 'The pool node id this message was generated from',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'message_template_id' => array(
'description' => 'The node id of the message template this message was generated from',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
),
'primary key' => array('id'),
'indexes' => array(
'CAMPAIGN' => array('campaign_fk'),
'MESSAGE_TEMPLATE' => array('message_template_id'),
'POOL' => array('pool_id'),
'USER' => array('user_id'),
),
);
// ---------------------------------------------------------------------------------
// CAMPAIGN
// ---------------------------------------------------------------------------------
$schema['myproject_entity_campaign'] = array(
'description' => 'The base table for myproject campaigns instances',
'fields' => array(
'id' => array(
'description' => 'The primary identifier for a campaign instance',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'schedule' => array(
'description' => 'The schedule of the campaign in text format.',
'type' => 'text',
'not null' => TRUE,
),
'interruptible' => array(
'description' => 'Whether this campaign is interruptible or not',
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
),
'campaign_template_id' => array(
'description' => 'Node id of campaign template this instance was created from',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'product_fk' => array(
'description' => 'Primary key of product instance this campaign belongs to',
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
),
),
'primary key' => array('id'),
'indexes' => array(
'CAMPAIGN_TEMPLATE' => array('campaign_template_id'),
'PRODUCT' => array('product_fk'),
),
);
// ---------------------------------------------------------------------------------
// PRODUCT
// ---------------------------------------------------------------------------------
$schema['myproject_entity_product'] = array(
'description' => 'The base table for myproject product instances',
'fields' => array(
'id' => array(
'description' => 'The primary identifier for a product instance',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'start' => array(
'description' => 'Time when the campaign started',
'type' => 'datetime',
'not null' => TRUE,
),
'current_campaign_fk' => array(
'description' => 'Foreign key of currently running campaign instance',
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
),
'next_campaign_fk' => array(
'description' => 'Foreign key of campaign instance to run next',
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
),
),
'primary key' => array('id'),
'indexes' => array(
'CURRENT' => array('current_campaign_fk'),
'NEXT' => array('next_campaign_fk'),
),
);
return $schema;
}
When i try to install the module and thus create the schema, I always get the following notice (which I dont think is the main reason for failing):
Notice: Undefined index: datetime:normal in DatabaseSchema_mysql->processField() (line 200 of /Users/xxx/Repos/myproject/includes/database/mysql/schema.inc).
And the following error:
PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOT NULL COMMENT 'When the message should arrive at the user', `campaign_fk` IN' at line 6: CREATE TABLE {myproject_entity_message} ( `id` INT unsigned NOT NULL auto_increment COMMENT 'The primary identifier for a message instance', `weekendday` TINYINT NOT NULL DEFAULT 0 COMMENT 'Whether this message gets send on a weekendday(1) or on a workday(0)', `day` INT NOT NULL COMMENT 'Numbered day of campaign schedule', `content` TEXT NOT NULL COMMENT 'Message content', `time_to_send` NOT NULL COMMENT 'When the message should arrive at the user', `campaign_fk` INT unsigned NULL DEFAULT NULL COMMENT 'the foreign key of the campaign this message belongs to', `user_id` INT unsigned NOT NULL COMMENT 'The user id this message is generated for', `pool_id` INT unsigned NOT NULL COMMENT 'The pool node id this message was generated from', `message_template_id` INT unsigned NOT NULL COMMENT 'The node id of the message template this message was generated from', PRIMARY KEY (`id`), INDEX `CAMPAIGN` (`campaign_fk`), INDEX `MESSAGE_TEMPLATE` (`message_template_id`), INDEX `POOL` (`pool_id`), INDEX `USER` (`user_id`) ) ENGINE = InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT 'The base table for myproject message instances'; Array ( ) in db_create_table() (line 2684 of /Users/xxx/Repos/myproject/includes/database/database.inc).
I searched for errors in the schema definition but could not find any. Also I don't see what exactly is wrong with the sql statement.
My server setup is:
Apache/2.2.20 (Unix) mod_ssl/2.2.20 OpenSSL/0.9.8r DAV/2 PHP/5.3.6 with Suhosin-Patch
MySQL client version: mysqlnd 5.0.8-dev - 20102224 - $Revision: 308673 $
Does anyone see whats wrong here?
I think you need to use datetime:normal as the key for your datetime column type.
'time_to_send' => array(
'description' => 'When the message should arrive at the user',
'type' => 'datetime:normal',
'not null' => TRUE,
),
Other than that you can try specifying the column type as a MySQL DATETIME explicitly:
'time_to_send' => array(
'description' => 'When the message should arrive at the user',
'mysql_type' => 'DATETIME',
'not null' => TRUE,
),
The problem seems to stem from whether or not DATETIME is actually allowed in Drupal 7; see this discussion for a lot more details than I can fit in here :)

Saving drupal webform data to a database

Im using Drupal version 6.19 and the webform module in Drupal to create forms.I have two forms on my site.When the user submits the form, where in the drupal database are the entries saved for each form ?
Please help
Thank You
Just take a look at its database schema in webform.install.
...
$schema['webform_submitted_data'] = array(
'description' => 'Stores all submitted field data for webform submissions.',
'fields' => array(
'nid' => array(
'description' => 'The node identifier of a webform.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'sid' => array(
'description' => 'The unique identifier for this submission.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'cid' => array(
'description' => 'The identifier for this component within this node, starts at 0 for each node.',
'type' => 'int',
'size' => 'small',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'no' => array(
'description' => 'Usually this value is 0, but if a field has multiple values (such as a time or date), it may require multiple rows in the database.',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'default' => '0',
),
'data' => array(
'description' => 'The submitted value of this field, may be serialized for some components.',
'type' => 'text',
'size' => 'medium',
'not null' => TRUE,
),
),
'indexes' => array(
'nid' => array('nid'),
'sid_nid' => array('sid', 'nid'),
),
'primary key' => array('nid', 'sid', 'cid', 'no'),
);
...
You should use:
First, log in to mysql
After, use database_name;
You must replace with the name of the database
Then, show tables;
The last sentence will show you the tables of tha database
Now, select * from webform_submitted_data;
You should be able to see the data.
You can also view submitted data by navigating to Content Management > Webforms. I believe the data is serialized in the database, so if you have large forms its not an easy read.

Exposing a module's data to Views2 using its API

I'm forking the filefield_stats module to provide it with the ability of exposing data into the Views module via the API.
The filefield_stats schema is as follow:
function filefield_stats_schema() {
$schema['filefield_stats'] = array(
'fields' => array(
'fid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'Primary Key: the {files}.fid'),
'vid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'Primary Key: the {node}.vid'),
'uid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'The {users}.uid of the downloader'),
'timestamp' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'The timestamp of the download'),
'hostname' => array('type' => 'varchar', 'length' => 128, 'not null' => TRUE, 'default' => '', 'description' => 'The hostname downloading the file (usually IP)'),
'referer' => array('type' => 'text', 'not null' => FALSE, 'description' => 'Referer for the download'),
),
'indexes' => array('fid_vid' => array('fid', 'vid')),
);
return $schema;
}
Well, so I implemented the hook_views_api() in filefield_stats.module & added a filefield_stats.views.inc file in the module's root directory, here it is:
// $Id$
/**
* #file
* Provide the ability of exposing data to Views2, for filefield_stats module.
*/
function filefield_stats_views_data() {
$data = array();
$data['filefield_stats']['table']['group'] = t('FilefieldStats');
// Referencing the {node_revisions} table.
$data['filefield_stats']['table']['join'] = array(
'node_revisions' => array(
'left_field' => 'vid',
'field' => 'vid',
),
'files' => array(
'left_field' => 'fid',
'field' => 'fid',
),
'users' => array(
'left_field' => 'uid',
'field' => 'uid',
),
);
// Introducing filefield_stats table fields to Views2.
// vid: The node's revision ID which wrapped the downloaded file
$data['filefield_stats']['vid'] = array(
'title' => t('Node revision ID'),
'help' => t('The node\'s revision ID which wrapped the downloaded file'),
'relationship' => array(
'base' => 'node_revisions',
'field' => 'vid',
'handler' => 'views_handler_relationship',
'label' => t('Node Revision Reference.'),
),
);
// uid: The ID of the user who downloaded the file.
$data['filefield_stats']['uid'] = array(
'title' => t('User ID'),
'help' => t('The ID of the user who downloaded the file.'),
'relationship' => array(
'base' => 'users',
'field' => 'uid',
'handler' => 'views_handler_relationship',
'label' => t('User Reference.'),
),
);
// fid: The ID of the downloaded file.
$data['filefield_stats']['fid'] = array(
'title' => t('File ID'),
'help' => t('The ID of the downloaded file.'),
'relationship' => array(
'base' => 'files',
'field' => 'fid',
'handler' => 'views_handler_relationship',
'label' => t('File Reference.'),
),
);
// hostname: The hostname which the file has been downloaded from.
$data['filefield_stats']['hostname'] = array(
'title' => t('The Hostname'),
'help' => t('The hostname which the file has been downloaded from.'),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
'argument' => array(
'handler' => 'views_handler_argument_string',
),
);
// referer: The referer address which the file download link has been triggered from.
$data['filefield_stats']['referer'] = array(
'title' => t('The Referer'),
'help' => t('The referer which the file download link has been triggered from.'),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
'argument' => array(
'handler' => 'views_handler_argument_string',
),
);
// timestamp: The time of the download.
$data['filefield_stats']['timestamp'] = array(
'title' => t('Download Time'),
'help' => t('The time of the download.'),
'field' => array(
'handler' => 'views_handler_field_date',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort_date',
),
'filter' => array(
'handler' => 'views_handler_filter_date',
),
);
return $data;
} // filefield_stats_views_data()
According to the Views2 documentations this should work as a minimum, I think. But it doesn't! Also there is no error of any kind, when I come through the views UI, there's nothing about filefield_stats data. Any idea?
I think your problem is in the function name: hook_views_data(), it should be filefield_stats_views_data().
hook_views_api() should also be filefield_stats_views_api().
You always replace hook with your module name when implementing them in your own modules.
Missing field definitions in the code above and also wrong hook_views_api() implementation. A working API implementation example can be found here: http://drupalcode.org/sandbox/sepehr/1073868.git/tree/refs/heads/master:/modules/sms_panel_views

Categories