Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-43803

Let moodle recordset accept a callback for transforming data

XMLWordPrintable

      Creating this bug to discuss the idea from MDL-34055.

      There are situations where it would be useful to be able to transform each result from a recordset on the fly, as it is retrieved from the database.

      The initial use case I had was to be able to further process the results of a query before passing to the bulk insert function. This would allow us to replace this code:

      // Query to fetch certain userids
      $userids = $DB->get_recordset_sql(...);
       
      // Convert them into object ready to insert
      $dataobjects = array();
      foreach ($userids as $userid) {
          $dataobject = new stdClass();
          $dataobject->userid = $userid;
          $dataobject->otherdata = $data;
          $dataobjects[] = $dataobject;
      }
      $DB->insert_records('sometable', $dataobjects);
      

      which requires all data to be read into memory with something like this:

      // Query to fetch certain userids
      $userids = $DB->get_recordset_sql(...);
       
      // transform would accept an anonymous function or named callback.
      $userids->transform(function($record, $data) {
          $dataobject = new stdClass();
          // $record refers to the results of the recordset query
          $dataobject->userid = $record->userid;
          $dataobject->otherdata = $data;
          return $dataobject;
      });
       
      $DB->insert_records('sometable', $dataobjects);
      

            simoncoggins Simon Coggins
            simoncoggins Simon Coggins
            Votes:
            1 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved:

                Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.