Batch example
Let's say you need to do some operation with a large number of node and the script execution time is not enough.
In this case, you can increase the execution time of the script as follows:
set_time_limit ($ time); // $ time in seconds
This is, to put it mildly, not the most correct solution.
In this case, it is much more correct to implement this through batch .
Using batch is extremely simple. Let me give you an example.
Suppose we have an array of nid:
There is also a certain function that works with this array.
For example, we’ll just load and save the node.
Now we describe the function with batch.
We will divide the $ nids array into parts (5 elements each) and send to batch_example_nodes_resave ()
Now it’s enough to pass our array to batch_example_nodes_resave_batch () and see how everything works beautifully :)
ps I apologize for the double post;
In this case, you can increase the execution time of the script as follows:
set_time_limit ($ time); // $ time in seconds
This is, to put it mildly, not the most correct solution.
In this case, it is much more correct to implement this through batch .
Using batch is extremely simple. Let me give you an example.
Suppose we have an array of nid:
Copy Source | Copy HTML- $nids = array(
- 0 => nid,
- 1 => nid,
- …
- n => nid,
- );
There is also a certain function that works with this array.
For example, we’ll just load and save the node.
Copy Source | Copy HTML- function batch_example_nodes_resave($nids = array()){
- foreach ($nids as $nid){
- if (is_numeric($nid)){
- $node = node_load($nid);
- node_save($node);
- }
- }
- }
Now we describe the function with batch.
We will divide the $ nids array into parts (5 elements each) and send to batch_example_nodes_resave ()
Copy Source | Copy HTML- function batch_example_nodes_resave_batch($nids = array()){
- $operations = array();
- while($nids){
- $nids_part = array_splice($nids, 0, 5);
- $operations[] = array('batch_example_nodes_resave', array($nids_part ));
- }
- $batch = array(
- 'title' => t('Resave nodes'),
- 'operations' => $operations,
- );
- batch_set($batch);
- batch_process();
- }
Now it’s enough to pass our array to batch_example_nodes_resave_batch () and see how everything works beautifully :)
ps I apologize for the double post;