简体   繁体   English

HABTM网桥数据未保存

[英]HABTM bridge data not saving

This is my first attempt at a HABTM in CakePHP and it's not going as well as I'd hoped. 这是我第一次在CakePHP中尝试HABTM,但进展并不理想。

I have table foos and table bars . 我有表foos和表bars When a foo gets saved, I want to associate several bars with it. 当一个foo被保存时,我想与它关联几个bars I am attempting to do this with the bars_foos bridge. 我正在尝试使用bars_foos桥进行此bars_foos

I'm wanting to be able to save in a way that I can pass a foo along with a bunch of bars something like: 我希望能够以一种可以传递foo以及一堆bars的方式进行保存,例如:

array(2) {
  ["Foo"]=> array(1) {
    ["name"]=> string(7) "someFoo"
  }
  ["Bar"]=> array(4) {
    [0]=> array(1) {
      ["ID"]=> int(3)
    }
    [1]=> array(1) {
      ["ID"]=> int(9)
    }
    [2]=> array(1) {
      ["ID"]=> int(4)
    }
    [3]=> array(1) {
      ["ID"]=> int(15)
    }
  }
}

lets say someFoo gets created with ID 9... I'd want the following records would be added to the bars_foos table: 可以说创建了ID为9的someFoo ...我希望将以下记录添加到bars_foos表中:

+--------+----------+
| bar_ID | foo_ID   |
+--------+----------+
|      3 |        9 |
|      9 |        9 |
|      4 |        9 |
|     15 |        9 |
+--------+----------+

Currently nothing is happening in the bars_foos table, only the foos table is getting updated with the newly created "someFoo". 当前, bars_foos表中什么都没有发生,只有foos表被新创建的“ someFoo”更新。 The only time that this bridge should ever get updated is when creating a new Foo 唯一应该更新此网桥的时间是在创建新Foo

I attempted to follow the CakePHP documentation with my model: 我试图用我的模型遵循CakePHP文档

class Foo extends AppModel {
    public $primaryKey = "ID";

    public $hasAndBelongsToMany = array(
        'Bar' =>
            array(
                'className' => 'Bar',
                'joinTable' => 'bars_foos',
                'foreignKey' => 'foo_ID',
                'associationForeignKey' => 'bar_ID'
        )
    );
}

and using this in my controller... 并在我的控制器中使用它...

$this->Foo->saveAll($data); //$data looks like the Array above in the first code block

I've also tried with my $data in these formats based on things I've seen in searching for a solution: 我还根据在搜索解决方案中看到的内容尝试了以下格式的$data

array(1) {
  ["Foo"]=> array(3) {
    ["name"]=> string(7) "FooName"
    ["Bar"]=> array(2) {
      [0]=> array(1) {
        ["ID"]=> int(3)
      }
      [1]=> array(1) {
        ["ID"]=> int(2)
      }
    }
  }
}

and

array(2) {
  ["Foo"]=> array(1) {
    ["name"]=> string(7) "fooName"
  }
  ["Bar"]=> array(1) {
    ["Bar"]=> array(2) {
      [0]=> array(1) {
        ["ID"]=> int(3)
      }
      [1]=> array(1) {
        ["ID"]=> int(2)
      }
    }
  }
}

and got the same result (new foo gets created, but nothing gets inserted in the bars_foos table) 并得到相同的结果(创建了新的foo ,但未在bars_foos表中插入任何bars_foos

Finally got it working. 终于成功了。 It seems we should not explicitly put in the ID keys for the related table. 看来我们不应该显式地输入相关表的ID键。 This format worked for me: 这种格式对我有用:

array(2) {
  ["Foo"]=>
  array(1) {
    ["name"]=> string(7) "fooName"
  }
  ["Bar"]=> array(1) {
    ["Bar"]=> array(4) {
      [0]=> int(3)
      [1]=> int(2)
      [2]=> int(9)
      [3]=> int(7)
    }
  }
}

and that gives me my desired result in the bridge table: 这在桥接表中给了我我想要的结果:

+--------+----------+
| bar_ID | foo_ID   |
+--------+----------+
|      3 |        9 |
|      2 |        9 |
|      9 |        9 |
|      7 |        9 |
+--------+----------+

This also seems to have the same effect (not nesting the Bar id's twice) 这似乎也有相同的效果(不是两次嵌套Bar ID)

array(2) {
  ["Foo"]=>
  array(1) {
    ["name"]=> string(7) "fooName"
  }
    ["Bar"]=> array(4) {
      [0]=> int(3)
      [1]=> int(2)
      [2]=> int(9)
      [3]=> int(7)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM