试图在CakePHP 2.0中保存HABTM模型数据
我有一个需要使用HABTM的数据模型,如下所示:
surveys ( id int(11) NOT NULL AUTO_INCREMENT,user_id int(11) NOT NULL,title varchar(50) DEFAULT NULL,created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,modified timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',PRIMARY KEY (id),KEY user_id (user_id) ); questions ( id int(11) NOT NULL AUTO_INCREMENT,title varchar(50) NOT NULL,body text NOT NULL,KEY user_id (user_id) ); questions_surveys ( id int(11) NOT NULL AUTO_INCREMENT,survey_id int(11) NOT NULL,question_id int(11) NOT NULL,KEY survey_id (survey_id),KEY question_id (question_id) ); 和相关的外键: ALTER TABLE questions_surveys ADD CONSTRAINT questions_surveys_ibfk_1 FOREIGN KEY(survey_id) REFERENCES surveys(id); ALTER TABLE questions_surveys ADD CONSTRAINT questions_surveys_ibfk_2 FOREIGN KEY(question_id) REFERENCES questions(id); 问题和调查具有HABTM关系,因此一项调查有许多问题,一个问题可以在许多不同的调查中进行. 在Survey.php中: public $hasAndBelongsToMany = array( 'Question' => array( 'className' => 'Question','joinTable' => 'questions_surveys','foreignKey' => 'survey_id','associationForeignKey' => 'question_id' ) ); 在Question.php中: public $hasAndBelongsToMany = array( 'Survey' => array( 'className' => 'Survey','foreignKey' => 'question_id','associationForeignKey' => 'survey_id' ) ); 这是我从SurveysController.php添加的控制器: public function add() { $this->set('fields',$this->Survey->getFields()); $this->set('users',$this->Survey->User->find('list',array('fields' => array('id','username')))); $this->set('questions',$this->Question->find('list','body')))); if (!empty($this->data)) { $this->Survey->saveAll($this->data['Survey']); foreach($this->data['Question']['id'] as $question_id) { $newdata[] = array('Survey' => array('id' => $this->Survey->getInsertID()),'Question' => array('id' => $question_id)); } if ($this->Survey->saveAll($newdata)) { $this->Session->setFlash('The survey was successfully added!'); $this->redirect(array('action'=>'index')); } else { $this->Session->setFlash('Unable to add survey.'); } } } 首先保存新的调查,然后将每个question_survey添加到一个数组中,然后一次添加所有这些调查.数据如下所示: Array ( [0] => Array ( [Survey] => Array ( [id] => 17 ) [Question] => Array ( [id] => 1 ) ) [1] => Array ( [Survey] => Array ( [id] => 17 ) [Question] => Array ( [id] => 2 ) ) [2] => Array ( [Survey] => Array ( [id] => 17 ) [Question] => Array ( [id] => 3 ) ) [3] => Array ( [Survey] => Array ( [id] => 17 ) [Question] => Array ( [id] => 4 ) ) [4] => Array ( [Survey] => Array ( [id] => 17 ) [Question] => Array ( [id] => 5 ) ) [5] => Array ( [Survey] => Array ( [id] => 17 ) [Question] => Array ( [id] => 6 ) ) [6] => Array ( [Survey] => Array ( [id] => 17 ) [Question] => Array ( [id] => 7 ) ) [7] => Array ( [Survey] => Array ( [id] => 17 ) [Question] => Array ( [id] => 8 ) ) [8] => Array ( [Survey] => Array ( [id] => 17 ) [Question] => Array ( [id] => 9 ) ) [9] => Array ( [Survey] => Array ( [id] => 17 ) [Question] => Array ( [id] => 10 ) ) ) 我一直收到这个错误:
据我所知,一切都按照CakePHP标准命名,我尝试使用’with’=> ‘QuestionsSurvey’,但收到此错误:
并且’with’=> ‘QuestionsSurveys’,但得到同样的错误:
我已经尝试将模型三重奏转换为hasMany直通模型(不起作用,它只是说回到HABTM). 我已经为数据(CakePHP Saving Your Data)使用了各种不同的格式,但也没有运气. 我很难过.有人知道我做错了什么吗?此外,我为很长的条目和代码部分道歉,但我想确保彻底. 感谢您的时间! 解决方法
您的数据应如下所示:
array( 'Survey' => array( 'title' => 'example',),'Question' => array( (int) 0 => '1',(int) 1 => '2',) ) 保存数据使用: 谢谢 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |