【问题标题】:HABTM data not saving (cakephp)HABTM 数据未保存 (cakephp)
【发布时间】:2010-12-18 13:13:20
【问题描述】:

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

【问题讨论】:

    标签: cakephp save has-and-belongs-to-many


    【解决方案1】:

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

    你试过了吗……

    echo $form->input('Person'.....
    

    【讨论】:

      猜你喜欢
      • 2012-08-08
      • 2011-09-14
      • 2013-09-24
      • 1970-01-01
      • 2023-03-07
      • 2010-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多