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 ...@@ -39,7 +39,13 @@ class Task extends \SimpleORMap implements \PrivacyObject
'foreign_key' => 'user_id' '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'] = [ $config['has_many']['test_tasks'] = [
'class_name' => $config['relationTypes']['TestTask'], 'class_name' => $config['relationTypes']['TestTask'],
...@@ -63,25 +69,14 @@ class Task extends \SimpleORMap implements \PrivacyObject ...@@ -63,25 +69,14 @@ class Task extends \SimpleORMap implements \PrivacyObject
/** /**
* Retrieve the tests associated to this task. * Retrieve the tests associated to this task.
* @deprecated - use $this->tests instead.
* *
* @return SimpleORMapCollection the associated tests * @return SimpleORMapCollection the associated tests
*/ */
public function getTests() public function getTests()
{ {
$klass = $this->relationTypes['Test']; $this->initRelation('tests');
return $this->relations['tests'];
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'
]
)
);
} }
/** /**
......
...@@ -31,7 +31,14 @@ class Test extends \SimpleORMap implements \PrivacyObject ...@@ -31,7 +31,14 @@ class Test extends \SimpleORMap implements \PrivacyObject
$config['relationTypes'] = self::configureClassNames($config); $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'] = [ $config['has_many']['testtasks'] = [
'class_name' => $config['relationTypes']['TestTask'], 'class_name' => $config['relationTypes']['TestTask'],
...@@ -59,25 +66,14 @@ class Test extends \SimpleORMap implements \PrivacyObject ...@@ -59,25 +66,14 @@ class Test extends \SimpleORMap implements \PrivacyObject
/** /**
* Retrieve the tasks associated to this test. * Retrieve the tasks associated to this test.
* @deprecated - use $this->tasks instead.
* *
* @return SimpleORMapCollection the associated tasks * @return SimpleORMapCollection the associated tasks
*/ */
public function getTasks() public function getTasks()
{ {
$klass = $this->relationTypes['Task']; $this->initRelation('tasks');
return $this->relations['tasks'];
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'
]
)
);
} }
/** /**
...@@ -87,9 +83,7 @@ class Test extends \SimpleORMap implements \PrivacyObject ...@@ -87,9 +83,7 @@ class Test extends \SimpleORMap implements \PrivacyObject
*/ */
public function countTasks() public function countTasks()
{ {
$klass = $this->relationTypes['TestTask']; return count($this->testtasks);
return $klass::countBySql('test_id = ?', [$this->id]);
} }
/** /**
...@@ -102,9 +96,9 @@ class Test extends \SimpleORMap implements \PrivacyObject ...@@ -102,9 +96,9 @@ class Test extends \SimpleORMap implements \PrivacyObject
*/ */
public function addTask($task) public function addTask($task)
{ {
$klass = $this->relationTypes['TestTask']; $class = static::config('relationTypes')['TestTask'];
$testTask = $klass::create( $testTask = $class::create(
[ [
'test_id' => $this->id, 'test_id' => $this->id,
'task_id' => $task->id, 'task_id' => $task->id,
...@@ -113,6 +107,7 @@ class Test extends \SimpleORMap implements \PrivacyObject ...@@ -113,6 +107,7 @@ class Test extends \SimpleORMap implements \PrivacyObject
] ]
); );
$this->resetRelation('tasks');
$this->resetRelation('testtasks'); $this->resetRelation('testtasks');
return $testTask; return $testTask;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment