dsfs5420 2014-10-07 03:23 采纳率: 100%
浏览 21
已采纳

HABTM桥数据不保存

This is my first attempt at a HABTM in CakePHP and it's not going as well as I'd hoped.

I have table foos and table bars. When a foo gets saved, I want to associate several bars with it. I am attempting to do this with the bars_foos bridge.

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:

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:

+--------+----------+
| 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". The only time that this bridge should ever get updated is when creating a new Foo

I attempted to follow the CakePHP documentation with my model:

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:

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)

  • 写回答

1条回答 默认 最新

  • dpn4073 2014-10-08 03:19
    关注

    Finally got it working. It seems we should not explicitly put in the ID keys for the related table. 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)

    array(2) {
      ["Foo"]=>
      array(1) {
        ["name"]=> string(7) "fooName"
      }
        ["Bar"]=> array(4) {
          [0]=> int(3)
          [1]=> int(2)
          [2]=> int(9)
          [3]=> int(7)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退