I am seeking to create nodes from records in a table residing in the database used by my site.
The content type has already been created via the Fields UI and contains fields for address information email address, urls, images, etc. and the mapping from table columns to fields is known.
In the past, ie Drupal 6, I would have hooked into cron, written a script and used insert SQL queries against the node, node_revision, and the Content_type_specific table, parceling out the source table’s data elements accordingly.
This is not possible with D7, or course, due to the new manner in which data is stored.
One would think that the DB_Insert function should be used, but all the examples I've seen in the documentation do not explicitly address the issue of storing fields created via the Fields UI.Am I being too literal when I see the following:
$nid = db_insert('node')
->fields = array(
"city" =>$sometown,
"state" =>$somestate,
"zip" =>$somezip,)
Does "node" refer to the node table, which still exists, or to an entity. if an entity will the drupal core use the field array to handle the storage of the extended elements
Some examples I've seen refer to "dbg_insert('mytable')". This furthers my confusion as to whether the DB_insert function should be used or if there is some other method.
Comments
Why not build a node as an
Why not build a node as an object and use node_save()?
So instead of DB_insert
$node = stdClass();
$blah = db_query()
foreach ($blah as $yada) {
$node->title = $sometitle;
$node->type = $sometype;
$node->city = $somecity;
$node->state = $somestate;
$node->zip = $somezip;
$node->name_first = $somenf;
$node->name_last = $somenl;
node_save();
}
seems to easy
Haven't found any documentation on node_save in D7.
Also heard node_save had trouble with not releasing all memory on completion, thus running out of memory was an issue (may not be applicable since I dont have thousands of nodes to create)
You would pass $node to
You would pass $node to node_save(), ie node_save($node);
Also, your field names need to match whats in the content type, so if the fields are added through the UI for example the would start with 'field_'.
Here's an example : $node =
Here's an example :
db_insert parameter documentation
I am trying to figure out the same thing. I am quite surprised the relationships between "node", "content types", database tables, etc. is not better documented. My content type "store" I created with the field UI has not table "store"-anything in the database. The fields are all there with their own tables.
Help.
Bill
One should use node_save()
One should use node_save() which provides the API for saving nodes and avoid the underlying database representation which is subject to change.
entity reference field..
in addition to what Arshad has shown, if u have entity reference field in your content/node then you should use 'target_id' instead of value ..
I have a content type called 'Incentives' which are auto generated as soon as someone joins network..