我正在努力让这件事发挥作用:
Orders
belongsToMany Products
到ProductsOrders
ProductsOrders有一个复合键(order_id,product_id) - 此时,一切都运行良好。
我想要添加一个新的关联(ProductsOrders
hasMany Extras
),因为一个产品可以有几个数据(调味料,烘焙风格,饮料......)。
所以我用:
创建了Extras
模型
$this->belongsTo('Orders', [
'foreignKey' => 'order_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Products', [
'foreignKey' => 'product_id',
'joinType' => 'INNER'
]);
我在ProductsOrders模型中添加了以下内容
$this->hasMany("Extras", [
'foreignKey' => [
'order_id',
'product_id'
],
'bindingKey' => [
'order_id',
'product_id'
]
]);
修补订单实体时,似乎是正确的:(简化版)
object(App\Model\Entity\Order) {
'establishment_id' => (int) 1,
'state_id' => (int) 20,
'products' => [
(int) 0 => object(App\Model\Entity\Product) {
'id' => (int) 32,
'establishment_id' => (int) 1,
'category_id' => (int) 11,
'_joinData' => object(App\Model\Entity\OrdersProduct) {
'quantity' => (int) 4,
'price' => (float) 9,
'extras' => [
(int) 0 => object(App\Model\Entity\Extra) {
'title' => 'Sauces',
'choice' => 'Mayonaise',
'price' => (float) 0,
'[new]' => true,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'title' => true,
'choice' => true,
'price' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Extras'
}
],
'[new]' => true,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'quantity' => true,
'price' => true,
'tva' => true,
'extras' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'OrdersProducts'
},
}
]
}
问题是,在保存(成功)时,表格中没有额外数据......这很难过。
我在哪里弄错了?或者这仍然可能吗?
感谢avdance