Hi all,

What is the procedure to create a node programmatically? The node is having 4-5 CCK fields..

Another doubt:
In a custom module, I have this:

db_query("UPDATE {content_type_application} SET field_no_of_hits_value = %d WHERE nid = %d", ($my_obj->field_no_of_hits_value + 1), $my_obj->nid);

It works correctly. But
1) The value is reset-ing whenever I am editing the node.
2) The value is getting updated in the database, but in the Drupal site, its not showing the correct value until and unless I am clearing the Drupal cache :(

please help!

Comments

Kuldip Gohil’s picture

For insert -

<?php
// add node properties
$newNode = (object) NULL;
$newNode->type = '{NODE_TYPE}';
$newNode->title = '{NODE_TITLE}'
$newNode->uid = {USER_ID};
$newNode->created = strtotime("now");
$newNode->changed = strtotime("now");
$newNode->status = 1;
$newNode->comment = 0;
$newNode->promote = 0;
$newNode->moderate = 0;
$newNode->sticky = 0;

// add CCK field data
$newNode->field_{YOUR_CUSTOM_FIELD_1}[0]['value'] = '{DATA_1}';
$newNode->field_{YOUR_CUSTOM_FIELD_2}[0]['value'] = '{DATA_2}';

// save node
node_save($newNode);
?>

For Update You have to load node then update field value.

<?php
$update_node = node_load($nid);
$update_node->title = 'update title';
node_save($update_node);
?>

Thank you,
Kuldip Gohil

mry4n’s picture

How do I use this code create 1,000 nodes from a database?

Let's say I get 1,000 records from a CSV file someone sends me. I guess I would then import all of those records into a table created in my MySQL Drupal database. And then I would have to include code that loops through all the records in that table and create the nodes using your code.

But how does all of that get executed? If I put it in a module, do I need to call a hook, or will it just automatically execute?

-Mike

mry4n’s picture

It looks like the Node Export module is probably the way I should go:

http://drupal.org/project/node_export

This module allows users to export nodes and then import it into another Drupal installation, or on the same site. The idea is similar to the way you export/import Views or Content Types.

Only problem is I'm not importing from another Drupal site. But I can probably figure out what format I need to use in order to import the non-Drupal records into Drupal nodes.

gforce301’s picture

Node export won't do you any good as you are importing brand spanking new never existed before, nodes.

Kuldip gave you the answer. Take the code he posted and write yourself a parser for your csv file of 1000 nodes. Since I don't feel like actually writing the code for you, I'll put in pseudo code.

Foreach line in csv file do this
   read line into variable
   take information in variable and use code from kuldip to save node
Repeat until no more lines in csv file

It's not all that difficult.

matiaslezin’s picture

What if the $newNode->field_{YOUR_CUSTOM_FIELD_1}[0]['value'] = '{DATA_1}'; is a Select List text field? I tried using the code but when I view the node, instead of getting the label value I see the key value.

Help?

bfodeke’s picture

Your code works awesome!

But I'm having one small issue. I'm scraping another website and using that data to create nodes. I'm trying to use the published date of the scraped data as the created date of the node. For example, the published date is Jun 15, 2012, so I'm doing this:

<?php
$published = "Jun 15, 2012";
$date = strtotime($published); //which gives me the right timestamp

//Then I use this date to set the created & changed fields in the new node
//.......Rest of code
$newNode->created = $date;
$newNode->changed = $date;
//.......Rest of code
?>

But when I create a view and show the publish date, it's all today's date, not the value of $date from above. Any suggestions?

mohsentaleb’s picture

if you see the node_submit() source code, it checks for $node->date and if it's not set, time() is used. So, to work around this problem you could do:

<?php
$node->created = strtotime($date);
$node->date = format_date($node->created, 'custom', 'Y-m-d H:i:s O');
?>

See http://drupal.org/node/286069 for more info.

amrit_b’s picture

Thanks a lot Kuldip :)

Hope it works..

hansrossel’s picture

If you would for some reason use db_query to change fields directly in the database like in the original question, you can clear the cache for that node as follows:

db_query("DELETE FROM {cache_content} WHERE cid LIKE '%content:%d%'", $nid);