php - CakePHP - 保存 HABTM 失败
问题描述
我有看起来像这样的表。基本上 WarehousePlace 里面可以有很多 ItemType 并且 ItemType 可以被很多 WarehousePlace 使用。但是当我尝试保存时,我得到一个数据库错误:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'WarehousePlacesItemType.item_type_id' in 'field list'
SQL Query: SELECT `WarehousePlacesItemType`.`item_type_id` FROM `modules`.`warehouse_places_item_types` AS `WarehousePlacesItemType` WHERE `WarehousePlacesItemType`.`warehouse_place_id` = '22'
但是一切都是按照惯例完成的,调试数据也应该是这样,我看不出有什么问题。
仓库控制器
public function add() {
$this->load();
if ($this->request->is('post')) {
$data = array(
'WarehousePlace' => array(
'name' => $this->request->data['WarehousePlace']['name'],
),
'ItemType' => array(
'ItemType' => $this->request->data['ItemType']['ItemType']
)
);
$this->WarehousePlace->create();
if ($this->WarehousePlace->saveAll($data, array('validate' => 'first', 'deep' => true))) {
//success
}
if ($this->WarehousePlace->validationErrors) {
//validation
} else {
//error
}
}
}
表:
warehouse_places_item_types(warehouse_places_id,item_types_id) -> no primary key since CakePHP 2,
warehouse_places(id, name),
item_types(id, name)
楷模:
物品种类
public $hasAndBelongsToMany = array(
'WarehousePlace' => array(
'className' => 'WarehousePlace',
'joinTable' => 'warehouse_places_item_types',
'foreignKey' => 'item_type_id',
'associationForeignKey' => 'warehouse_place_id',
'unique' => 'keepExisting',
)
);
仓库地点
public $hasAndBelongsToMany = array(
'ItemType' => array(
'className' => 'ItemType',
'joinTable' => 'warehouse_places_item_types',
'foreignKey' => 'warehouse_place_id',
'associationForeignKey' => 'item_type_id',
'unique' => 'keepExisting',
)
);
解决方案
推荐阅读
- caching - Vue 利用浏览器缓存静态资产
- javascript - 如果您没有文档参考,是否可以将 StartAt 与 Firestore 查询一起使用
- java - 我们可以将 Spark 流用于基于时间的事件吗
- swift - 如何使用coredata从上到下对tableview项目进行排序
- mysql - 如何在mysql中更改日期格式
- java - 如果我们定义 WebDriver 驱动程序,则在使用 TestNG 时;全局然后不创建该类的对象,我们如何能够在方法下使用驱动程序?
- android - 单击列表视图项时从 SQLite 数据库中获取行 ID
- hadoop - SemanticException 10072: 数据库不存在 (Sqoop)
- python - 如何在python中检测具有nan值的字符串
- javascript - 如何正确运行 firebase 功能?