Reputation: 1000
I'm trying to make this thing work :
Orders
belongsToMany Products
through ProductsOrders
ProductsOrders has a composite key (order_id, product_id) - At this point, everything work perfectly.
What I would like is to add a new association (ProductsOrders
hasMany Extras
) because a product can have a couple of data (sauces, kaking style, drink, ...).
So I've created the Extras
model with :
$this->belongsTo('Orders', [
'foreignKey' => 'order_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Products', [
'foreignKey' => 'product_id',
'joinType' => 'INNER'
]);
And I added the following in the ProductsOrders model
$this->hasMany("Extras", [
'foreignKey' => [
'order_id',
'product_id'
],
'bindingKey' => [
'order_id',
'product_id'
]
]);
When patching the Order Entity, it seems to be correct : (simplified version)
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'
},
}
]
}
The problem is, when saving (with success), there is no Extras data in table ... That's sad.
Where did I make a mistake ? Or is this still possible ?
Thanks in avdance
Upvotes: 0
Views: 62