php - Relating Objects in CakePHP -


i have users , have courses. trying create "join" button users when they're viewing course list in order subscribe them courses.

i told use

class course extends appmodel {   public $hasandbelongstomany = array('user'); } 

and

class user extends appmodel {   public $hasandbelongstomany = array('course'); } 

but don't know how implement actual button , posting of info table called courses_users

where implement this?

thanks in advance!

you want sent student id , course id saveall() method. through courses controller follows:

<?php class coursescontroller extends appcontroller {      public function join($courseid) {         $data = array(             'course' => array(                 'id' => $courseid             ),             'student' => array(                 'id' => $this->auth->user('id') // id of logged in user             )         );          if ($this->course->saveall($data)) {             $this->session->setflash( __('successfully joined course.'));         }         else {             $this->session->setflash( __('error joining course.'));         }          $this->redirect('/');     } } 

this simple example. you’ll need tweak app, , add validation etc.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -