【发布时间】:2017-06-26 10:54:48
【问题描述】:
我想知道,是否可以通过数据库中的匹配表 在 CakePHP3 中创建 OneToMany 关系。 这就是我的数据库的样子: Showcase of DB Schema
这是我的 ItemA 表:
$this->belongsTo('ItemB', [
'foreignKey' => 'item_a_id',
'targetForeignKey' => 'item_b_id',
'joinTable' => 'item_a_item_b'
]);
这是我的 ItemB 表:
$this->belongsToMany('ItemA', [
'foreignKey' => 'item_b_id',
'targetForeignKey' => 'item_a_id',
'joinTable' => 'item_a_item_b'
]);
但是,当我在模板中为 ItemA 创建一个控件时,它仍然给了我一个多选。
echo $this->Form->control('item_b._ids', ['options' => $item_b, 'empty' => true]);
当我将此表单更改为单选时,所选对象将不会被传递。我被困在多重选择中:(
这是 Cake 中 DB Schema 的正确实现吗?我必须使用“通过”选项吗?我很困惑...
Edit#1:如果我用belongsToMany 而不是belongsTo 配置ItemA,它会起作用。但这将是多对多关系。
【问题讨论】:
标签: mysql orm associations cakephp-3.0