I have a custom module to import nodes from a .txt file. (Drupal 7). My problem is I have a lot of nodes to import and this function takes so much time.
$operations = array();
$items = array();
$limit = 100;
$i = 0;
$regs = db_query("SELECT * FROM {table_import_apunte} WHERE field_processed = :processed", array(':processed' => 0));
foreach ($regs as $row) {
$row = (array) $row;
if ($i < $limit) {
$items[] = $row;
$i += 1;
} else {
$operations[] = array('csvImporter_create_nodes', array($items, 'apunte'));
$items = array();
$items[] = $row;
$i = 1;
}
}
if (!empty($items)) {
$operations[] = array('csvImporter_create_nodes', array($items, 'apunte'));
$batch = array(
'title' => t('Importing %name table', array('%name' => drupal_strtoupper('apunte'))),
'init_message' => t('Starting Import...'),
'progress_message' => t('Processed @current out of @total.'),
'error_message' => t('There was a problem importing the file.'),
'operations' => $operations,
'finished' => 'csvImporter_batch_finished',
);
batch_set($batch);
}
csvImporter_create_nodes
use save_node to create nodes. In this foreach
, I divide $regs
(which contain my data to import) in package of 100 elements and call the csvImporter_create_nodes
function. I need to make this foreach
faster and more efficient.
I have a version in Drupal 6 which is faster. This is the extract code:
while (($row = db_fetch_array($regs)) !== FALSE) {
if ($i < $limit) {
$items[] = $row;
$i += 1;
} else {
$operations[] = array('_importer_batch_create_nodes', array($items, $table));
$items = array();
$items[] = $row;
$i = 1;
}
}
but it uses db_fetch_array
and I can't use it in Drupal 7.
-
\$\begingroup\$ There is another SE website you might find helpful, it is Drupal Answers, drupal.stackexchange.com/questions. \$\endgroup\$pacmaninbw– pacmaninbw ♦2016年04月29日 15:19:44 +00:00Commented Apr 29, 2016 at 15:19
1 Answer 1
Drupal 7 replaced the Drupal 6 procedural interface with an object oriented interface. Several of the result interfaces are discussed here. They didn't exactly remove db_fetch_array()
, they put it into the object oriented interface. You can use your while loop with
while (($row = regs->fetchAssoc()) !== FALSE) {
The following is a direct quote from drupal.org.
Example - Drupal 6:
<?php
$result = db_fetch_array(db_query("SELECT * FROM {boxes} WHERE bid = %d", $bid));
?>
Drupal 7:
<?php
$result = db_query("SELECT * FROM {block_custom} WHERE bid = :bid", array(':bid' => $bid))->fetchAssoc();
?>