php - cant add new record in HABTM table -
i want add new record in table called tutorssubject. table created habtm relationship of tutor , subject. anyway load subject names in text box , can select subject name cant add new record. need set id of tutor_id. there 3 fields in tutorssubject table called id,tutor_id , subject_id. need select in 1 field subject_id list of names found in subject table, , set tutor_id manually.
currently able select list of subject names cant add new record subject name , set tutor_id.
error: sqlstate[23000]: integrity constraint violation: 1452 cannot add or update child row: foreign key constraint fails
class tutorssubjectscontroller extends appcontroller {
public function tutoradd() {
$this->loadmodel('subject'); $options['recursive']=-1; $options['fields']=array('subject.name'); $te2= $this->subject->find('list',$options); debug($te2); $this->set( 'te',$te2); //put edit , post new record if ($this->request->is('post')) { $this->request->data['tutorssubject']['tutor_id']=2; debug($this->request->data); if ($this->tutorssubject->save($this->request->data)) { $this->session->setflash(__('your post has been updated.')); return $this->redirect(array('controller' => 'tutors','action' => 'tutordetails')); } $this->session->setflash(__('unable update post.')); } //view works fine displays list echo $this->form->create('tutorssubject', array('type' => 'post')); echo $this->form->input('tutorssubject.subject_id', array('options' => $te)); echo $this->form->end('save post'); //model subject public $hasandbelongstomany = array( 'tutor' => array( 'classname' => 'tutor', 'jointable' => 'tutors_subjects', 'foreignkey' => 'subject_id', 'associationforeignkey' => 'tutor_id', 'unique' => 'keepexisting', 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', 'finderquery' => '', 'deletequery' => '', 'insertquery' => '' ) ); //model tutor public $hasandbelongstomany = array( 'subject' => array( 'classname' => 'subject', 'jointable' => 'tutors_subjects', 'foreignkey' => 'tutor_id', 'associationforeignkey' => 'subject_id', 'unique' => 'keepexisting', 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', 'finderquery' => '', 'deletequery' => '', 'insertquery' => '' ), );
Comments
Post a Comment