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);
}
}
}