1

我有两个模型相关的 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。我做错了什么?

4

1 回答 1

7

也许您的$this->data数组应该有一个“人”部分,而不是复数“人”

你有没有尝试过...

echo $form->input('Person'.....
于 2009-11-21T03:24:37.010 回答