Posted by smiletrl on July 21, 2012 at 8:51am
Hi, I just want to create nodes with node_save($node). These $node data comes from csv file.
<?php
while (($data = fgetcsv($handle)) != FALSE) {
$new_node = (object) NULL;
$new_node->type = 'scores';
$new_node->title = $data[0];
$new_node->language = 'und';
$new_node->field_score_team_name = array(
'und' => array(
array(
'value' => $data[1],
),
),
);
$new_node->field_score_pts = array(
'und' => array(
array(
'value' => $data[2],
),
),
);
$new_node->field_score_home = array(
'und' => array(
array(
'value' => $data[10],
),
),
);
node_object_prepare($new_node);
node_save($new_node);
}
?>These fields handling way will give
Uncaught exception thrown in session handler.As for the field property of $node, I also tried
<?php
$new_node->field_score_home = $data[10];
?>This won't produce exception. But does nothing with fields.
I guess I can't figure out
field_attach_insert within node_save.There should be a format for entity fields to get saved before invoking
field_attach_op.So how should this be? Anyone could help? Thank you!
Comments
Not sure if this helps but I
Not sure if this helps but I always just follow the instructions from here:
http://timonweb.com/how-programmatically-create-nodes-comments-and-taxon...
Hi, tce, thank you for your
Hi, tce, thank you for your help. I just figured out that I forgot eliminating the header line from the csv file.
Your recommended link is helpful. Thank you.
Not sure will this solve ur problem
if u r about to save a new node that u have to specify the is_new property as new
i.e $new_node->is_new=true;
Need Drupal help?
Reach Me
Drupal Developer , Themer.
Thank you for your reply. In
Thank you for your reply. In node_save, we get this
<?php// Determine if we will be inserting a new node.
if (!isset($node->is_new)) {
$node->is_new = empty($node->nid);
}
?>
which tells if property is_new is not set, it will check $node->nid. If property nid is not set, it will set $node->is_new true. In this way, I guess it's not necessary to set $new_node->is_new outside node_save, since $node->nid is NULL.
Anyway, Thanks for your reply.