HABTM 数据未保存 (cakephp)
我有两个与 HABTM 相关的模型(文档和人员)。
class Person extends AppModel {
var $name = 'Person';
var $hasAndBelongsToMany = array(
'Document' => array(
'className' => 'Document',
'joinTable' => 'documents_people',
'foreignKey' => 'person_id',
'associationForeignKey' => 'document_id',
'unique' => false
)
);
class Document extends AppModel {
var $name = 'Document';
var $hasAndBelongsToMany = array(
'Person'=>array(
'className' => 'Person',
'joinTable' => 'documents_people',
'foreignKey' => 'document_id',
'associationForeignKey' => 'person_id',
'unique' => false
)
);
我有文档的添加视图,其中为与文档相关的每个人填充了一个复选框。
echo $form->input('People', array('type'=>'select', 'multiple'=>'checkbox', 'options'=>$people, 'label' => 'People: '));
这是来自控制器的线路,应该进行保存。
$this->Document->create();
if ($this->Document->saveAll($this->data)) {
我注意到数据没有保存到 Documents_people 表中。所以,我转储了$this->data。
文档部分如下所示:
[Document] => Array
(
[file_name] => asdasd
[tags] => habtm
[People] => Array
(
[0] => 6
[1] => 12
[2] => 15
)
[image] => img/docs/2009-11-19-233059Jack.jpg
)
这些是我想要与此文档关联的人员的 ID。然而,没有任何内容被传输到documents_people。我做错了什么?
I have two models related HABTM (documents and people).
class Person extends AppModel {
var $name = 'Person';
var $hasAndBelongsToMany = array(
'Document' => array(
'className' => 'Document',
'joinTable' => 'documents_people',
'foreignKey' => 'person_id',
'associationForeignKey' => 'document_id',
'unique' => false
)
);
class Document extends AppModel {
var $name = 'Document';
var $hasAndBelongsToMany = array(
'Person'=>array(
'className' => 'Person',
'joinTable' => 'documents_people',
'foreignKey' => 'document_id',
'associationForeignKey' => 'person_id',
'unique' => false
)
);
I have the add view of documents populated with one checkbox for each person that will be related to the document.
echo $form->input('People', array('type'=>'select', 'multiple'=>'checkbox', 'options'=>$people, 'label' => 'People: '));
This is the line from the controller that is supposed to be doing the saving.
$this->Document->create();
if ($this->Document->saveAll($this->data)) {
I noticed that the data was not getting saved into the documents_people table. So, I dumped $this->data.
The document portion looks like this:
[Document] => Array
(
[file_name] => asdasd
[tags] => habtm
[People] => Array
(
[0] => 6
[1] => 12
[2] => 15
)
[image] => img/docs/2009-11-19-233059Jack.jpg
)
Those are the ids of the people I want associated with this document. However, nothing is transferred to documents_people. What have I done wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许你的
$this->data
数组应该有一个“Person”部分,而不是复数“People”你试过吗...
Perhaps your
$this->data
array should have a 'Person' section, not a plural 'People'Have you tried...