-
Improvement
-
Resolution: Fixed
-
Minor
-
3.7
-
MOODLE_37_STABLE
-
MOODLE_37_STABLE
-
As discussed with stronk7, the new preload SQL functions are not well placed.
This issue moves them to a new class at \core\dml\table which provides functions to generate the SQL field select, and to pull data for that table from a mixed result stdClass.
In short, these functions are intended as helpers to allow you to select data from multiple tables into usable objects in a single query. This is something that we do regularly and we do it in a variety of painful ways.
This is an attempt to produce a single solution which is reuable, and easy to read.
|
$posttable = new \core\dml\table('forum_posts', 'p', 'p_');
|
$discussiontable = new \core\dml\table('forum_discussions', 'd', 'd_');
|
$forumtable = new \core\dml\table('forum', 'f', 'f_');
|
$coursetable = new \core\dml\table('course', 'c', 'c_');
|
|
$sql = "SELECT " .
|
implode(', ', [
|
$posttable->get_field_select(),
|
$discussiontable->get_field_select(),
|
$forumtable->get_field_select(),
|
$coursetable->get_field_select(),
|
]) . " FROM " . $posttable->get_from_sql() .
|
" JOIN " . $discussiontable->get_from_sql() . " ON d.id = p.discussion " .
|
" JOIN " . $forumtable->get_from_sql() . " ON f.id = d.forum " .
|
" JOIN " . $coursetable->get_from_sql() . " ON c.id = f.course " .
|
" WHERE p.userid = :author";
|
|
$data = $DB->get_record_sql($sql, ['author' => $USER->id]);
|
|
foreach ($data as $datum) {
|
$post = $posttable->extract_fields_from_result($data);
|
$discussion = $discussiontable->extract_fields_from_result($data);
|
$forum = $forumtable->extract_fields_from_result($data);
|
$course = $coursetable->extract_fields_from_result($data);
|
}
|
- Discovered while testing
-
MDL-65180 Review all the dml and ddl @package and @category phpdocs
-
- Closed
-