【发布时间】:2018-05-08 20:19:57
【问题描述】:
在 OctoberCMS 中,我有三个表:
学生、课程和订阅(学生 ID、课程 ID、活动)
当学生订阅课程时,在管理员激活它之前它不会被激活。这是合乎逻辑的。但是当管理员从后端打开一个课程表时,他可以选择学生订阅这个课程,然后他们被添加到订阅表中。在这里我想更新字段:自动激活。
我怎样才能做到这一点?
我写了这段代码,但是没有用:
public function afterSave()
{
foreach($this->students as $student)
{
$student->pivot->is_activated = true;
$student->pivot->save;
}
}
编辑:
这是我在@Hardik Satasiya 建议之后的所有代码:
class Course extends Model
{
public $belongsToMany = [
'students'=>[
'Sunsoft\Courses\Models\Student',
'table'=>'sunsoft_courses_student_course',
'order'=>'users.name',
'scope'=>'students',
'timestamps' => true,
'pivot'=>['id', 'is_activated'],
'pivotModel'=>'Sunsoft\Courses\Models\StudentCourse',
],
];
public function afterSave()
{
foreach($this->students as $student)
{
$student->pivot->is_activated = true;
$student->pivot->save;
$student->save;
}
}
}
class Student extends User
{
public $belongsToMany = [
'courses'=>[
'sunsoft\courses\models\Course',
'table'=>'sunsoft_courses_student_course',
'order'=>'sunsoft_courses_courses.name',
'pivot'=>['id', 'is_activated'],
'timestamps' => true,
'pivotModel'=>'Sunsoft\Courses\Models\StudentCourse',
],
];
}
class StudentCourse extends Model
{
public $belongsTo= [
'course'=>['sunsoft\courses\models\Course'],
'student'=>['sunsoft\courses\models\Student', 'scope'=>'students', 'order'=>'users.name'],
];
}
这是我得到的错误:
http://prntscr.com/jhrfzu
【问题讨论】:
-
您在哪个模型中编写了这段代码?你能用代码显示三个表之间的关系吗?
标签: octobercms octobercms-backend october-form-controller