0

我正在尝试使用 drupal 6.26 将自定义内容类型 - 节点存储在 drupal 数据库中。我已经创建了模式表,但是当我执行表单的提交按钮时,出现以下错误:

用户警告:键“PRIMARY”查询的重复条目“0-0”:service_form_submit /* admin:service_form_submit */ INSERT INTO service (nid, vid,uid, fallbackservice_ids, category, biblebasic, blocked, reason_blocked, created, begin, stop ) VALUES (0, 0, 0, '', '', '', 0, '', 0, 0, 0 ) 在 /var/www/drupal/modules/service/service.module 第 261 行。

并且至少没有条目存储在数据库中,nid、vid、uid 或我输入的其他值似乎都不可用。

我如何通过 $node 是否正确?

function service_form(&$node) {
function service_form_submit($node) {

这里是我如何使用 $node 的完整代码?

<?php
/**
* Implementation of hook_node_info().
*/
function service_node_info() {
  // We return an array since a module can define multiple node types.
  return array(
    'service' => array(
      'name' => t('Service'), // Required.
      'module' => 'service',  // Required. Prefix of the callback functions to look for: service_validate(), service_insert(), service_delete()
      'description' => t('Offer your service to the church.'), // Required.
      'has_title' => TRUE,
      'title_label' => t('Please enter your Service Name'),
      'has_body' => TRUE,
      'body_label' => t('Service Description'),
      'min_word_count' => 2,
      'locked' => TRUE
    )
    // todo here we add more node types later
  );
}


function service_form(&$node) {

// Get metadata for this node type
  // (we use it for labeling title and body fields).
  // We defined this in service_node_info().
  $type = node_get_types('type', $node);

  $form['general'] = array(
    '#type' => 'fieldset',
    '#title' => t('General Service Information')
  );
  // service name
  $form ['general']['title'] = array(
    '#type' => 'textfield',
    '#title' =>  check_plain($type->title_label), // 'Service Name',  //
    '#required' => TRUE,
    '#default_value' => $node->title,
    '#weight' => -5,
    '#maxlength' => 255,
  );
  // service description
  $form ['general']['body_filter']['body'] = array(
    '#type' => 'textarea',
    '#title' =>  check_plain($type->body_label),  // 'Service Description', //
    '#default_value' => $node->body,
    '#rows' => 6,
    '#required' => TRUE
  );
//  Filter options
// $form['body_filter']['filter'] = filter_form($node->format);

// todo replace with taxonomy  - ask forum
  $form ['general'] ['category'] = array(
    '#multiple' => '0',
    '#required' => '1',
    '#key_type_toggled' => '0',
    '#description' => t('Category of the service  '),
    '#default_value' => isset($node->category) ? $node->category : '',
    '#weight' => '3',
    '#type' => 'select',
    '#options' => array(
      '1' => t('Apostelservice'),
      '2' => t('Social Service'),
      '3' => t('Praying Service'),
   ),
  '#multiple_toggle' => '1',
  '#title' => t('Category'),
   );

     $form['availabilty'] = array(
    '#type' => 'fieldset',
    '#title' => t('Service Availabilty') ,
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    );


   $form ['availabilty'] ['service_start'] = array(
   '#type' => 'date_popup',
   '#title' => t('Start service on'),
   '#size' => 20,
   '#maxlength' => 20,
   '#default_value' => isset($node->service_start) ? $node->service_start : '',
   );

   $form['availabilty']['service_stop'] = array(
   '#type' => 'date_popup',
   '#title' => t('Terminate Service at'),
   '#size' => 20,
   '#maxlength' => 20,
   '#default_value' => isset($node->service_stop) ? $node->service_stop : '',
   );

   // fallback service
   $form ['availabilty']['fallback_service'] = array(
  '#multiple' => '1',
  '#required' => '0',
  '#key_type_toggled' => '0',
  '#description' => t('Choose a fallback services to avoid bottlnecks'),
  '#default_value' => array(
   '0' => '1',
  ),
  '#type' => 'select',
  '#options' => array(
    '1' => t(' random / no fallback service'),
    '2' => t(' Fallback Service 2'),
    '3' => t(' Fallback Service 3'),
  ),
  '#multiple_toggle' => '1',
  '#title' => t('Fallback Services'),
  );


   $form ['availabilty']['blocked'] = array(
  '#default_value' => array(
  ),
  '#required' => '0',
  '#key_type_toggled' => '0',
  '#description' => t('The service can be disabled - blocked due to different reasons e.g. misuse, clarification needed'),
  '#weight' => '1',
  '#type' => 'checkboxes',
  '#options' => array(
    'one' => t('Block Service'),
   ),
  '#title' => t('Block Service'),
   );
   $form ['availabilty']['reason_blocked'] = array(
   '#required' => '0',
   '#input_format' => '1',
   '#description' => t('Please enter a reason why the Service is disabled, if this is the case.'),
   '#weight' => '2',
   '#type' => 'textarea',
   '#title' => t('Reason of blocked Service'),
   );

   $form['location'] = array(
   '#type' => 'fieldset',
   '#title' => t('Service location information')
   );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Register your service'),
  );

  return $form;
}
**
* Handle submission of the service form and saving
* of the data to the database.
*/
function service_form_submit($node) {

db_query("INSERT INTO {service} (nid, vid,uid,  fallbackservice_ids , category , biblebasic ,
blocked , reason_blocked , created , begin , stop) VALUES (%d, %d, %d, '%s', '%s', '%s',  %d, '%s', %d, %d, %d )",
$node->nid, $node->vid,$node->uid,  $node->fallbackservice_ids, $node->category,
  $node->biblebasic, $node->blocked, $node->reason_blocked,  $node->created, $node->begin, $node->stop);
  }
?>
4

1 回答 1

1

您使用自定义模块执行此操作有什么特别的原因吗?为什么不直接创建一个新的内容类型?

如果你真的想用你自己的模块来做,你看过示例模块吗?你真正想看的代码在这里

于 2013-01-02T16:07:07.273 回答