-
Improvement
-
Resolution: Won't Do
-
Minor
-
None
-
2.6.1
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);
|
- has a non-specific relationship to
-
MDL-44078 Proposal: API standard in Moodle that uses autoloading (hooks)
-
- Closed
-