не могу добавить новую запись в таблицу HABTM

Я хочу добавить новую запись в таблицу с именем TutorsSubject. Эта таблица создана из отношения HABTM Tutor и Subject. В любом случае, я загружаю имена субъектов в текстовое поле и могу выбрать имя субъекта, но не могу добавить новую запись. Мне нужно установить идентификатор tutor_id.
В таблице tutorsSubject есть 3 поля с именами id, tutor_id и subject_id. Мне нужно только выбрать в 1 поле, которое является subject_id из списка имен, найденных в таблице тем, а затем установить tutor_id вручную.

В настоящее время я могу выбрать из списка имен субъектов, но не могу добавить новую запись с именем субъекта и установить tutor_id.

Ошибка: SQLSTATE [23000]: нарушение ограничения целостности: 1452 Невозможно добавить или обновить дочернюю строку: ограничение внешнего ключа не выполнено

Класс TutorsSubjectsController расширяет AppController {

публичная функция 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 is for edit and post for 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 to update your post.'));
}//View which works fine as it displays the 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' => ''
),

);

0

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector