我正在尝试保存一个与BelongsToMany相关联的实体,即订购BelongsToMany产品。 我的联合表包含我需要保存的重要数据,即使产品ID重复也是如此。联合表中尚未创建唯一键。 但是,patchEntity阻止了我这样做,因为它会删除重复的产品。
OrdersTable.php
class OrdersTable extends Table
{
public function initialize(array $config)
{
parent::initialize($config);
$this->belongsToMany('Products', [
'foreignKey' => 'order_id',
'targetForeignKey' => 'product_id',
'joinTable' => 'orders_products',
'through' => 'OrdersProducts'
]);
}
}
这是我的POST,您可以在其中看到ID为1772的产品被多次插入。
'products' => [
(int) 0 => [
'_joinData' => [
'id' => '50',
'order_id' => '',
'product_name' => '- name -',
'exception_id' => '',
'exception_name' => '',
'weft' => '100',
'warp' => '100',
'manufacture_name' => 'AC',
'real_cost' => '1.2',
'ordered_quantity' => '32',
'received_quantity' => '2',
'difference' => '30'
],
'id' => '1772'
],
(int) 1 => [
'_joinData' => [
'id' => '',
'order_id' => '',
'exception_id' => '1',
'weft' => '100',
'warp' => '100',
'manufacture_name' => 'AC',
'real_cost' => '1.2',
'ordered_quantity' => '111',
'received_quantity' => '1',
'difference' => '0'
],
'id' => '1772'
]
]
修补实体后,我得到了 (我已删除了无用或敏感的数据)
'products' => [
(int) 0 => object(App\Model\Entity\Product) {
'id' => (int) 1772,
'_joinData' => object(App\Model\Entity\OrdersProduct) {
'id' => (int) 50,
'exception_id' => null,
'manufacture_id' => null,
'order_id' => null,
'product_id' => (int) 1772,
'sorting' => (int) 1,
'price' => null,
'real_cost' => (float) 1.2,
'ordered_quantity' => (int) 32,
'received_quantity' => (int) 2,
'weft' => (float) 100,
'warp' => (float) 100,
'weft_with_shrinkage' => null,
'warp_with_shrinkage' => null,
'weft_with_shrinkage_fold' => null,
'warp_with_shrinkage_fold' => null,
'[new]' => false,
},
}
],
这对我不好。我的客户希望多次添加相同的产品。
有没有一种方法可以防止patchEntity()删除重复项?