I have taken two models: "Course" for course registration and "StudentCourseRelation" for displaying selected course by student from course table.
Course :-
id(PK),
course_name,
course_details,
duration.
StudentCourseRelation :-
id(PK),
course_id(FK),
user_id(FK),
created_date.
After clicking on "register" button in Course registration…I want that particular entry should be saved in StudentCourseRelation table.
Please guide me!
Could U please provide more detailed explanation?
In the View for the course
echo CHtml::button('Register', array( 'submit'=>array('course/register',array('id'=>$model->id)), 'confirm' => 'Are you sure you want to register for this course?' ) );
Course Controller
public function accessRules() {
return array(
array('allow', // allow all users to perform register function put in the appropriate place just need to make sure you authorize the action.
'actions' => array('register'),
'users' => array('*'),
),
)
}
public function register($id) {
$registerUser = new StudentCourseRelation;
//get the id from the button
$registerUser->course_id = $id;
$registerUser->save();
if ($registerUser->save()){
//prevent double submitting of form
$this->redirect('index');
}
}
StudentCourseRelation Model
//create time will always be the time it was created
//user_id will always be the users id
//so we can put it in a before save function so we don't have
//to write code to handel it every time
public function beforeSave() {
if ($this->isNewRecord)
$this->created_date = new CDbExpression('NOW()'); //assuming you're using MySQL and datetime field
$this->user_id = Yii::app()->user->id;
return parent::beforeSave();
}
Don’t know if this works as i just wrote it in the comment box for you. It should but it may need to be modified a little bit.
Thank you for your response.
I tried your code…it is working!