I'm trying to create nodes programmatically so that I can import a spreadsheet into the database and have each item be a node. I have a business directory module that I have written for a client, and I have a spreadsheet of businesses that I am trying to import into the database. Each business is a node, and has a record in the node table as well as the business directory table.

I've been working on this all day, and I have made some progress, but the results aren't what I need. I am using the node_save() function, which successfully writes to the node table as many times as there are records. However, no records are written to the business directory table. The api page for the function wasn't really helpful, so I went searching for an example here. I found one and adapted it to my situation. Here is the code:

// loop through all the import records
	$imported_query = db_query("select * from {business_import}");
	while($imported = db_fetch_array($imported_query))
	{
		// into variables so system can write to business directory and node tables
		$biz_name = $imported['Name'];
		$address_attention = $imported['Address Attention'];
		$address1 = $imported['Address 1'];
		$address2 = $imported['Address 2'];
		$city = $imported['City'];
		$state = $imported['State'];
		//$zip = $imported['Zip'];
		$property_address = $imported['Property Address'];
		
		// node stuff
		$node = new StdClass();
 
		$node->title = $biz_name;
		
		$node->created = time();
		$node->changed = $node->created;
		
		$node->status = 1;
		$node->promote = 0;
		$node->sticky = 0;
		
		$node->type = 'business';
		
		$node->format = 1; // Filtered HTML
		
		// node type specific information
		$node->street_address = $address1 . ' ' . $address2;
		$node->city = $city;
		$node->state = $state;
		$node->is_premium_listing = 0;
		
		node_save($node);
		
		// $nid=$node->nid;
	}

I was under the impression that the node_save function invokes the insert hook, but I guess I'm not clear on that.

Am I missing something (I am, but I don't know what), and how do I fix it?

Thanks,

Caleb

Comments

nevets’s picture

Have you looked at the Node import module

BladeRider’s picture

EDIT - just seen nevets reply ninja'd in there, if you'd rather not get into the PHP'ness I'm on about then try out what he suggests first!

What's missing from your above code is some means of actually writing your extra information to your business directory table i.e. some SQL statements.

This is achieved by implementing three small hook functions:

  • hook_insert
  • hook_update
  • hook_delete

You will have noticed that these names mirror SQL statements that do the same work. It's in these hooks that your db_query() calls to execute the correct SQL will be placed.

Have you actually written a node extension module to do this, or do you just have a table sitting there? If you have, then these are the hooks to implement. If you've not, then the best thing to do is to have a peek at the node_example.module code (http://api.drupal.org/api/file/developer/examples/node_example.module/5).

You'll need to pay attention to the hooks above, and make sure you implement the hook_node_info as well. All this in essence tells Drupal that you're extending the basic node type with extra data - which should suit your needs perfectly. Ignore the other functions for the moment, the ones mentioned above will get you your "core" functionality.

(You should also implement a hook_install function to create your own tables if you're not already doing so).

If this is a bit much to digest, please say so!

thecalebrogers’s picture

OK, what I've got is a fully-functioning, basically stand-alone, module that acts as a business directory, all with it's own supporting tables and all the necessary node functions. That's not the issue I'm having. What I'm having trouble with is importing a spreadsheet of businesses into my business directory table, and the node table.

In my system, each business is a node, so each record from this spreadsheet is going to be a node. There are 1269 records, so I don't want to do this manually. I have already taken the spreadsheet and imported it into a temp table so that I can loop through those records and write the appropriate information from each record to the node table and the business table, and make sure that each business is a node. I was able to do this and get all of the records into the node table, but not my business table. Additionally, even though the records were in the node table, with node IDs, nothing showed up in the content admin area for those records. Does that mean these were or were not nodes in the drupal framework? I don't know.

After research, it seemed that node_save was the way to go, but I'm missing something. I don't know if it's that I'm missing another function, or that I need more for the node_save function.

Here is the section of code in the node_save function that led me to believe that node_save was all I needed:

 // Call the node specific callback (if any):
  if ($node->is_new) {
    node_invoke($node, 'insert');
    node_invoke_nodeapi($node, 'insert');
  }
  else {
    node_invoke($node, 'update');
    node_invoke_nodeapi($node, 'update');
  }

That is from this page: http://api.drupal.org/api/function/node_save/5

So, based on all of that information, does anyone see what I'm missing? I don't really want to use the node import module, because I have a custom node type to deal with, and it would probably be easier to make this node_save setup I've got work instead of modifying the node import module to suit my needs. I could be wrong, but that's the impression I got after looking at the node import module.

Thanks,
Caleb
http://www.cube-escape.com

thecalebrogers’s picture

Anyone have any ideas?

Thanks,
Caleb
http://www.cube-escape.com

matt v.’s picture

chaoscube,

Any chance you were trying to call hook_insert from within hook_install or hook_enable? I'm running into a similar problem where hook_insert isn't getting called when I do a node_save from either hook_install or hook_enable. If I put the same block of code elsewhere in my module, it does call the hook_insert function, as I'd expect.

I'm trying to get to the bottom of the issue, but I'd love to get some confirmation of what I'm seeing.

Thanks!
--
Drupal Theme Developer’s Cheat Sheet | 45 Screencasts to Get You Kicking Ass with Drupal