By amrit_b on
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
node_save()
For insert -
For Update You have to load node then update field value.
Thank you,
Kuldip Gohil
Importing From Database
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
Node Export
It looks like the Node Export module is probably the way I should go:
http://drupal.org/project/node_export
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.
Node export won't do you any
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.
CCK Select List field
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?
Your code works awesome! But
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:
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?
Problem with node date while using node_submit($node)
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:
See http://drupal.org/node/286069 for more info.
Thanks a lot Kuldip :) Hope
Thanks a lot Kuldip :)
Hope it works..
If you would for some reason
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:
KOBA - Drupal Webdesign & Webdevelopment