Hi, I just want to create nodes with node_save($node). These $node data comes from csv file.

  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

$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

tce’s picture

Not sure if this helps but I always just follow the instructions from here:

http://timonweb.com/how-programmatically-create-nodes-comments-and-taxon...

smiletrl’s picture

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.

chandan chaudhary’s picture

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

Acquia Certified Developer

Backend Frontend and DevOps.

smiletrl’s picture

Thank you for your reply. In node_save, we get this

// 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.