3
\$\begingroup\$

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.

rolfl
98.1k17 gold badges219 silver badges419 bronze badges
asked Apr 28, 2016 at 8:51
\$\endgroup\$
1
  • \$\begingroup\$ There is another SE website you might find helpful, it is Drupal Answers, drupal.stackexchange.com/questions. \$\endgroup\$ Commented Apr 29, 2016 at 15:19

1 Answer 1

1
\$\begingroup\$

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();
?>
answered Apr 28, 2016 at 13:05
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.