Invoke preBatchMethod before determining the number of items

Created on 22 July 2024, 5 months ago
Updated 7 August 2024, 4 months ago

Problem/Motivation

While trying to create a script to update nodes, I was creating a function to create some nodes to operate on. It seemed to make sense to do so during the preBatchMethod() function.

Doing so causes the following output

 [warning] Undefined array key "multi_run_state_key" BatchOperations.php:735
Codit Batch Operations:  SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null: INSERT INTO "key_value" ("name", "collection", "value") VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array
(
    [:db_insert_placeholder_0] => 
    [:db_insert_placeholder_1] => state
    [:db_insert_placeholder_2] => i:0;
)

This is due to the order of the steps in initSandbox(), which first gathers the items to process and then runs the preBatchMethod().

Because the nodes that I want the script to collect haven't yet been created, the sandbox comes back as empty, and the array key 'multi_run_state_key' isn't created.

Proposed resolution

Move the call to the preBatchMethod():

 public function initSandbox(array &$sandbox) {
    if (empty($sandbox['total'])) {
      // Sandbox has not been initiated.
      $this->checkAndSetCanRun();
      $pre_msg = $this->preBatchMethod($sandbox);
      $this->batchOpLog->appendLog("preBatchMethod: {$pre_msg}");

      $sandbox['items_to_process'] = $this->getItemsToProcess();

etc.

✨ Feature request
Status

Closed: won't fix

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States skyriter

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024