php - Creating a User who has-and-belongs-to-many existing Courses with a CakePHP form -
i have app 2 associated models: user , course, related habtm association.
there registration form new user may enter username , select courses part of (from list of existing courses in database), form saves new users - doesn't save join table.
the join table (courses_users
) has columns course_id
, user_id
, , 2 models this:
// user.php class user extends appmodel { public $name = 'user'; public $hasandbelongstomany = array( 'courses' => array( 'classname' => 'course', 'jointable' => 'courses_users', 'foreignkey' => 'user_id', 'associatedforeignkey' => 'course_id' ) ); } // course.php class course extends appmodel { public $name = 'course'; public $hasandbelongstomany = array( 'users' => array( 'classname' => 'user', 'jointable' => 'courses_users', 'foreignkey' => 'course_id', 'associatedforeignkey' => 'user_id' ) ); }
in addition, controller action:
// identificationcontroller.php public function register() { if ($this->request->is('post')) { $data = $this->request->data; $username = $data['user']['username']; $saved = $this->user->save($data, array('deep' => true)); //debug($data); if ($saved) { $this->_set_new_user_session($username); //$log = $this->user->getdatasource()->getlog(false, false); //debug($log); $this->redirect(array('controller' => 'users', 'action' => 'index')); } } // not redirecting $courses = $this->course->find('list', array('course.name')); //debug($courses); $this->set(compact('courses')); }
and form, sans container divs:
<?php echo $this->form->create('user', array( 'inputdefaults' => array( 'label' => false, 'div' => false ), 'url' => '/identification/register' )); echo $this->form->input('username', array( 'error' => false, 'autofocus' => true, 'required' => true, 'pattern' => '[a-za-z0-9]{3,16}' )); if ($this->form->isfielderror('username')) { echo $this->form->error('username', null, array('wrap' => 'small', 'class' => 'error')); } echo $this->form->input('course.course', array( 'error' => false, 'required' => true )); if ($this->form->isfielderror('courses')) { echo $this->form->error('course', null, array('wrap' => 'small', 'class' => 'error')); } echo $this->form->button('register', array( 'div' => false, 'type' => 'submit' )); echo $this->form->end(); ?>
when call debug($data), right data seems passed form controller:
array( 'user' => array( 'username' => 'test63apd' ), 'course' => array( 'course' => array( (int) 0 => '1' ) ) )
but nothing happens join table, , there no mention of join table in datasource log:
array( 'log' => array( (int) 0 => array( 'query' => 'begin', 'params' => array(), 'affected' => null, 'numrows' => null, 'took' => null ), (int) 1 => array( 'query' => 'insert `xray2`.`users` (`username`) values ('test06apd')', 'params' => array(), 'affected' => (int) 1, 'numrows' => (int) 1, 'took' => (float) 1 ), (int) 2 => array( 'query' => 'commit', 'params' => array(), 'affected' => (int) 1, 'numrows' => (int) 1, 'took' => (float) 1 ) ), 'count' => (int) 3, 'time' => (float) 2 )
am missing obvious here, or there quirk of cake have yet discover?
you're telling cake save primary model data. need change line:-
$saved = $this->user->save($data, array('deep' => true));
to:-
$saved = $this->user->saveassociated($data, array('deep' => true));
saveassociated()
tells cake save current model , associated data. shouldn't need pass array('deep' => true)
saving data directly associated model. better (and safer) use:-
$saved = $this->user->saveassociated($data);
update
there issue data being saved not using alias defined in association data. when cake attempts save associated data can't see any. according code user
model has , belongs many courses
(plural) save data uses course
(singular). therefore, form should be:-
echo $this->form->input('courses.courses', array( 'error' => false, 'required' => true ));
it should noted cake naming conventions use singular forms model names, better use course
in association rather courses
. if change association can simplified following:-
public $hasandbelongstomany = array( 'course' );
cake understand how handle join table , foreign keys conform naming convention. wouldn't need change form.
Comments
Post a Comment