MDL-75165 reportbuilder: entity method to override multiple aliases.

This commit is contained in:
Paul Holden 2022-07-05 16:43:14 +01:00
parent f8d28e4ca6
commit f1c3aca7ea
3 changed files with 57 additions and 6 deletions

View File

@ -139,7 +139,8 @@ abstract class base {
}
/**
* Override the default alias for given database table used in entity queries
* Override the default alias for given database table used in entity queries, to avoid table alias clashes that may occur
* if multiple entities of a report each define the same default alias for one of their tables
*
* @param string $tablename
* @param string $alias
@ -155,6 +156,21 @@ abstract class base {
return $this;
}
/**
* Override multiple default database table aliases used in entity queries as per {@see set_table_alias}, typically when
* you're adding an entity multiple times to a report you'd want to override the table aliases in the second instance to
* avoid clashes with the first
*
* @param array $aliases Array of tablename => alias values
* @return self
*/
final public function set_table_aliases(array $aliases): self {
foreach ($aliases as $tablename => $alias) {
$this->set_table_alias($tablename, $alias);
}
return $this;
}
/**
* Returns an alias used in the queries for a given table
*

View File

@ -340,13 +340,9 @@ class course_test extends advanced_testcase {
/**
* Test entity table alias
*/
public function test_table_alias(): void {
public function test_get_table_alias(): void {
$courseentity = new course();
$this->assertEquals('c', $courseentity->get_table_alias('course'));
$courseentity->set_table_alias('course', 'newalias');
$this->assertEquals('newalias', $courseentity->get_table_alias('course'));
}
/**
@ -361,6 +357,16 @@ class course_test extends advanced_testcase {
$courseentity->get_table_alias('nonexistingalias');
}
/**
* Test setting table alias
*/
public function test_set_table_alias(): void {
$courseentity = new course();
$courseentity->set_table_alias('course', 'newalias');
$this->assertEquals('newalias', $courseentity->get_table_alias('course'));
}
/**
* Test invalid entity set table alias
*/
@ -372,6 +378,34 @@ class course_test extends advanced_testcase {
$courseentity->set_table_alias('nonexistent', 'newalias');
}
/**
* Test setting multiple table aliases
*/
public function test_set_table_aliases(): void {
$courseentity = new course();
$courseentity->set_table_aliases([
'course' => 'newalias',
'context' => 'newalias2',
]);
$this->assertEquals('newalias', $courseentity->get_table_alias('course'));
$this->assertEquals('newalias2', $courseentity->get_table_alias('context'));
}
/**
* Test setting multiple table aliases, containing an invalid table
*/
public function test_set_table_aliases_invalid(): void {
$courseentity = new course();
$this->expectException(coding_exception::class);
$this->expectExceptionMessage('Coding error detected, it must be fixed by a programmer: Invalid table name (nonexistent)');
$courseentity->set_table_aliases([
'course' => 'newalias',
'nonexistent' => 'newalias2',
]);
}
/**
* Test entity name
*/

View File

@ -28,6 +28,7 @@ Information provided here is intended especially for developers.
* The base aggregation `format_value` method has a `$columntype` argument in order to preserve type during aggregation. When
defining column callbacks, strict typing will now be preserved in your callback methods when the column is being aggregated
* The method `get_joins()` in the base entity class is now public, allowing for easier joins within reports
* New method `set_table_aliases` in base entity class, for overriding multiple table aliases in a single call
* The following local helper methods have been deprecated, their implementation moved to exporters:
- `audience::get_all_audiences_menu_types` -> `custom_report_audience_cards_exporter`
- `report::get_available_columns` -> `custom_report_column_cards_exporter`