Posted by easycombvba on January 17, 2013 at 10:51am
Hi,
A problem again..
I'm trying to write a record into a table that i added using hook_schema();
<?php
function uc_auctionschema_schema(){
return array(
'uc_auction_automatic_bids' => array(
'description' => 'Keeps track of the automatic biddings.',
'fields' => array(
'autobidid' => array(
'description' => 'The ID number of the automatic bid.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'nid' => array(
'description' => 'The {node}.nid of the product',
'type' => 'int',
'unsigned' => TRUE,
'default' => 0,
),
'uid' => array(
'description' => 'The {user}.uid of the bidder.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'maxcredits' => array(
'description' => 'The maximum amount of credits to use for this.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'maxamount' => array(
'description' => 'The maximum amount for the auto bidder..',
'type' => 'numeric',
'precision' => 15,
'scale' => 3,
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array('autobidid'),
),
);
}
?>The table got correctly installed after running update.php.
Now when i try to insert a record, nothing happens.. Not even an error in the log..
This is my code to insert:
<?php
function uc_auction_autobidform_submit($form, &$form_state){
global $user;
$nodeid = $form_state['build_info']['args'][0]->nid;
$userid = $user->uid;
if($nodeid&&$userid&&($form_state['values']['credit_autobid_creditlimit']<>''||$form_state['values']['credit_autobid_amountlimit']<>'')){
$record = array (
"nid" => $nodeid,
"uid" => $userid,
"maxcredits" => $form_state['values']['credit_autobid_creditlimit'],
"maxamount" => $form_state['values']['credit_autobid_amountlimit'],
);
drupal_write_record('uc_auction_automatic_bids', $record);
drupal_set_message(t('Deze is opgeslagen!'));
}
}
?>Suggestions?
Comments
Are you sure about IF
Are you sure about IF condition in 'form_submit' making true and call to drupal_write_record() is made with
the record?
Tushar Bodake
Yes it returns true, the
Yes it returns true, the message is being displayed
Founder of oozyo.com/modelgezocht.be
Main designer/developer at easycom
Schema hook
Unless your module is called "uc_auctionschema", the hook_schema function is misnamed.
--
The Manual | Troubleshooting FAQ | Tips for posting.
Thats not where the problem
Thats not where the problem is.
<?phpfunction uc_auction_update_7003(){
drupal_install_schema('uc_auctionschema');
}
?>
is how its get updated, just to clarify the table exists.
Founder of oozyo.com/modelgezocht.be
Main designer/developer at easycom
Really! If your hook_schema
Really!
If your hook_schema is incorrectly named, drupal_write_record won't know about the schema. drupal_install_schema _will_ create the tables, but it _won't_ add the definitions to the registered schema's. That's where hook_schema comes in.
From the API doc (emphasis added).
--
The Manual | Troubleshooting FAQ | Tips for posting.
To elaborate on that,
To elaborate on that, drupal_write_record() calls drupal_get_schema(), which in turn calls drupal_get_complete_schema(), in which the following code resides:
<?phpforeach (module_implements('schema') as $module) {
// Cast the result of hook_schema() to an array, as a NULL return value
// would cause array_merge() to set the $schema variable to NULL as well.
// That would break modules which use $schema further down the line.
$current = (array) module_invoke($module, 'schema');
// Set 'module' and 'name' keys for each table, and remove descriptions,
// as they needlessly slow down cache_get() for every single request.
_drupal_schema_initialize($current, $module);
$schema = array_merge($schema, $current);
}
?>
The first line checks for all modules that implement hook_schema(). module_implements() gets all enabled modules, then looks for MODULENAME_schema(). So as Heine said, if your hook_schema() implementation isn't the same name as your module, it will not be found, which means drupal_write_record() doesn't have a table to which it can write its data.
Jaypan We build websites
Alright i understand it and
Alright i understand it and i'll check this in my next update.
But...
<?phpdb_insert('uc_auction_automatic_bids')
->fields(array(
'nid' => $nodeid,
'uid' => $userid,
'maxcredits' => $creditlimit,
'maxamount' => $amountlimit,
))
->execute();
?>
The above code does work, and does create the record in the database, (wich as i mentioned does exist)
Founder of oozyo.com/modelgezocht.be
Main designer/developer at easycom
You are comparing apples to
You are comparing apples to oranges. The mechanisms behind drupal_write_record() and db_insert() are different things.
Jaypan We build websites