I have the following error: When I create a new node from the new data of my external database (inserting a new row in the external database), the new node is duplicated (but it doesn't have type, language, user); this causes the following error:

Invalid argument supplied for foreach() en /path/web/my_proyect/sites/all/modules/cck/content.module on line 1284.

With my other module, I first extract all the rows of my external database, and convert them in nodes.

I don't know why; I debugged my code, but I don't understand that it is causing the problem.

This is the code of my module.

/* Implement of Hook_cron */
function node_db_extension_cron(){
  inspectAddHoteles();
  drupal_flush_all_caches();
}

function getHotelFromDrupal() {
  $type = '[my_type]';
  $query_nid = "SELECT count(nid) count FROM node WHERE type LIKE '%s'";
  $result_start = db_query($query_nid, $type);
  $cantidad = 0;

  while ($row = db_fetch_object($result_start)) {
      $cantidad = (int) ($row->count);
  }

  return $cantidad;
}

function getHotelFromExternal() {
  db_set_active('external');

  $result = db_query("SELECT count(id) count FROM [my_table]");
  $cantidad = 0;

  while ($row = db_fetch_object($result)) {
      $cantidad = (int) ($row->count);
  }

  db_set_active('default');

  return $cantidad;
}

function inspectAddHoteles() {
  global $user;

  $cantd_actual_hotel = getHotelFromDrupal();
  $cantd_final_hotel = getHotelFromExternal();

  /* With the count of nid of drupal and id of db external, i verify if exist new rows  because the count of nid of content and id of rows of my db externa should be equals if don't exists new rows on my db external */

  if ($cantd_actual_hotel < $cantd_final_hotel) {
    db_set_active('external');

    $cant_add = $cantd_final_hotel - $cantd_actual_hotel;
    $query = "SELECT * FROM [my_table] ORDER BY id DESC LIMIT %d";
    $additional = db_query($query, $cant_add);

    db_set_active('default');

    while ($row = db_fetch_object($additional)) {
      $node = new stdClass();
      $node->uid = $user->uid;
      $node->type = 'my_type';
      $node->title = $row->title;
      $node->body = $row->body;
      $node->created = time();
      $node->changed = $node->created;
      $node->promote = 0;
      $node->sticky = 0;
      $node->format = 2;
      $node->status = 1; 
      $node->language = 'es';
      //All field of my content type

      $nodo_1 = node_submit($node);
      node_save($nodo_1);
    }
  }
}
link|improve this question

70% accept rate
1  
Consider accepting some answers to your questions. 0% accept rate is very poor. For help, see How do I ask questions here? – Laxman13 Feb 22 at 17:15
@Laxman13 Thanks for you help, I didn't know about that. :D – Eduardo Gabriel Cabrera Japa Feb 22 at 18:40
1  
This seems to be quite a localised question.... – Chapabu Feb 22 at 18:54
1  
@EduardoGabrielCabreraJapa Be careful with deleting your questions; if you keep doing that, you will not be able anymore to ask new questions because there is a block for who asks low quality questions, and that is triggered also by deleting your own questions. – kiamlaluno Feb 22 at 19:40
@kiamlaluno ok.. i didn't know this, too. I thought I could delete the questions without any problems. – Eduardo Gabriel Cabrera Japa Feb 22 at 21:20
feedback

1 Answer

The function node_submit() contains the node_save() function. So you call it twice.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.