diff --git a/lib/models/eTask/Task.php b/lib/models/eTask/Task.php index 68d4a208a5812bab91c24757469a1765268b2dcb..989915d7863475f7a0e4e250bba6a723dd3514fc 100644 --- a/lib/models/eTask/Task.php +++ b/lib/models/eTask/Task.php @@ -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']; } /** diff --git a/lib/models/eTask/Test.php b/lib/models/eTask/Test.php index a6279a018b7c85f09cd60ad7cf9c298ef72dc143..2f35cd42c99948ec39da9069b61abd8e8763e545 100644 --- a/lib/models/eTask/Test.php +++ b/lib/models/eTask/Test.php @@ -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;