Skip to content
Snippets Groups Projects
Commit 3a9652b2 authored by Elmar Ludwig's avatar Elmar Ludwig
Browse files

use class configuration instead of `$this->relationTypes`, re #1473

Merge request studip/studip!1018
parent d3f79db0
No related branches found
No related tags found
No related merge requests found
......@@ -39,7 +39,13 @@ class Task extends \SimpleORMap implements \PrivacyObject
'foreign_key' => 'user_id'
];
$config['additional_fields']['tests']['get'] = 'getTests';
$config['has_and_belongs_to_many']['tests'] = [
'class_name' => $config['relationTypes']['Test'],
'assoc_foreign_key' => 'id',
'thru_table' => 'etask_test_tasks',
'thru_key' => 'task_id',
'thru_assoc_key' => 'test_id'
];
$config['has_many']['test_tasks'] = [
'class_name' => $config['relationTypes']['TestTask'],
......@@ -63,25 +69,14 @@ class Task extends \SimpleORMap implements \PrivacyObject
/**
* Retrieve the tests associated to this task.
* @deprecated - use $this->tests instead.
*
* @return SimpleORMapCollection the associated tests
*/
public function getTests()
{
$klass = $this->relationTypes['Test'];
return \SimpleORMapCollection::createFromArray(
$klass::findThru(
$this->id,
[
'thru_table' => 'etask_test_tasks',
'thru_key' => 'task_id',
'thru_assoc_key' => 'test_id',
'assoc_foreign_key' => 'id',
'order_by' => 'ORDER BY etask_tests.chdate ASC'
]
)
);
$this->initRelation('tests');
return $this->relations['tests'];
}
/**
......
......@@ -31,7 +31,14 @@ class Test extends \SimpleORMap implements \PrivacyObject
$config['relationTypes'] = self::configureClassNames($config);
$config['additional_fields']['tasks']['get'] = 'getTasks';
$config['has_and_belongs_to_many']['tasks'] = [
'class_name' => $config['relationTypes']['Task'],
'assoc_foreign_key' => 'id',
'thru_table' => 'etask_test_tasks',
'thru_key' => 'test_id',
'thru_assoc_key' => 'task_id',
'order_by' => 'ORDER BY position'
];
$config['has_many']['testtasks'] = [
'class_name' => $config['relationTypes']['TestTask'],
......@@ -59,25 +66,14 @@ class Test extends \SimpleORMap implements \PrivacyObject
/**
* Retrieve the tasks associated to this test.
* @deprecated - use $this->tasks instead.
*
* @return SimpleORMapCollection the associated tasks
*/
public function getTasks()
{
$klass = $this->relationTypes['Task'];
return \SimpleORMapCollection::createFromArray(
$klass::findThru(
$this->id,
[
'thru_table' => 'etask_test_tasks',
'thru_key' => 'test_id',
'thru_assoc_key' => 'task_id',
'assoc_foreign_key' => 'id',
'order_by' => 'ORDER BY etask_test_tasks.position ASC'
]
)
);
$this->initRelation('tasks');
return $this->relations['tasks'];
}
/**
......@@ -87,9 +83,7 @@ class Test extends \SimpleORMap implements \PrivacyObject
*/
public function countTasks()
{
$klass = $this->relationTypes['TestTask'];
return $klass::countBySql('test_id = ?', [$this->id]);
return count($this->testtasks);
}
/**
......@@ -102,9 +96,9 @@ class Test extends \SimpleORMap implements \PrivacyObject
*/
public function addTask($task)
{
$klass = $this->relationTypes['TestTask'];
$class = static::config('relationTypes')['TestTask'];
$testTask = $klass::create(
$testTask = $class::create(
[
'test_id' => $this->id,
'task_id' => $task->id,
......@@ -113,6 +107,7 @@ class Test extends \SimpleORMap implements \PrivacyObject
]
);
$this->resetRelation('tasks');
$this->resetRelation('testtasks');
return $testTask;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment