OctoberCMS - Update a field in a related table when inserting

和自甴很熟 提交于 2021-02-11 13:23:29

问题


In OctoberCMS, I have three tables:
Students, Courses and Subscriptions (studentID, courseID, active)
When student subscribes to a course, it's not activated until the admin activate it. That is logical. But When admin opens a course form from backend, he can select students to subscribe in this course, then they are added to subscriptions table. Here I would like to update the field: active automatically.
How can I dot that?
I wrote this code, but it didn't work:

    public function afterSave()
    {
        foreach($this->students as $student)
        {
            $student->pivot->is_activated = true;
            $student->pivot->save;
        }
    }

Edit:

This is all my code after @Hardik Satasiya advice:

    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'],
        ];      
    }

This is the error I get:
http://prntscr.com/jhrfzu


回答1:


For this you need to define proper relation ship inside the Courses Model

define Pivot Model => Subscriptions you already did it.

Now define proper Relation in Courses. Model

public $belongsToMany = [
    ....
    'students_pivot_model' => [
        'HardikSatasiya\Plugin\Models\Students', // replace model
        'table' => 'yournamespace_course_student', // replace tb name
        'pivot' => ['is_activated'],
        'timestamps' => true, // if you added times-stamp support
        'pivotModel' => 'HardikSatasiya\Plugin\Models\Subscriptions',
    ],
    ....
];

Now to set is_activated manually

public function afterSave()
{
    foreach($this->students_pivot_model as $pivot_model)
    {
        $pivot_model->pivot->is_activated = true;
        // no validations
        $pivot_model->pivot->forceSave(); 
    }
}

this should work. If any doubts please comment.



来源:https://stackoverflow.com/questions/50241697/octobercms-update-a-field-in-a-related-table-when-inserting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!