我有一个 products
, attributes
,和 product_attributes
table。每个产品可以有多个 attributes
这些关系储存在 product_attributes
,到目前为止还不错。但是一个产品可以多次具有相同的属性。我只能将不同的属性链接到一个产品。
例如:
奥迪(产品)有轮子(属性):数量(joindata)=2;值(joindata)=“前”;
和
奥迪(产品)有轮子(属性):数量(joindata)=2;值(joindata)=“后”;
仅保存值为rear的属性wheel,前轮将丢失。
控制器中没有错误。
这是 $this->request->getData()
```
/src/Controller/ProductsController.php (line 62)
[
'type_id' => '12',
'name' => 'Audi',
'thumbnail' => '',
'image' => '',
'attributes' => [
(int) 0 => [
'unique_id' => '9',
'_joinData' => [
'amount' => '2',
'value' => '1',
'information' => 'front'
]
],
(int) 1 => [
'unique_id' => '9',
'_joinData' => [
'amount' => '2',
'value' => '1',
'information' => 'rear'
]
]
]
]
这是 `$product` 修补实体后:
object(App\Model\Entity\Product) {
'type_id' => (int) 12,
'name' => 'Audi',
'thumbnail' => '',
'image' => '',
'attributes' => [
(int) 0 => object(App\Model\Entity\Attribute) {
'unique_id' => (int) 9,
'category_id' => (int) 8,
'name' => 'VDSL2',
'unit' => '',
'_joinData' => object(App\Model\Entity\ProductsAttribute) {
'amount' => (int) 2,
'value' => (float) 1,
'information' => 'rear',
'[new]' => true,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'amount' => true,
'value' => true,
'information' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'ProductsAttributes'
},
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'_joinData' => true
],
'[original]' => [
'_joinData' => [
'amount' => '2',
'value' => '0',
'information' => 'front'
]
],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Attributes'
},
(int) 1 => object(App\Model\Entity\Attribute) {
'unique_id' => (int) 9,
'category_id' => (int) 8,
'name' => 'VDSL2',
'unit' => '',
'_joinData' => object(App\Model\Entity\ProductsAttribute) {
'amount' => (int) 2,
'value' => (float) 1,
'information' => 'rear',
'[new]' => true,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'amount' => true,
'value' => true,
'information' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'ProductsAttributes'
},
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'_joinData' => true
],
'[original]' => [
'_joinData' => [
'amount' => '2',
'value' => '0',
'information' => 'front'
]
],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Attributes'
}
],
'[new]' => true,
'[accessible]' => [
'type_id' => true,
'name' => true,
'thumbnail' => true,
'image' => true,
'type' => true,
'attributes' => true
],
'[dirty]' => [
'type_id' => true,
'name' => true,
'thumbnail' => true,
'image' => true,
'attributes' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Products'
}
productcontroller添加函数:
public function add(){
$product = $this->Products->newEntity();
if ($this->request->is('post')) {
$product = $this->Products->patchEntity($product, $this->request->getData(), [
'associated' => [
'Attributes',
'Attributes._joinData'
]
]);
/debug($this->request->getData());
debug($product);
die();/
if ($this->Products->save($product)) {
$this->Flash->success(__('The product has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The product could not be saved. Please, try again.'));
}
$types = $this->Products->Types->find('list', ['limit' => 200]);
$attributes = $this->Products->Attributes->find('list', ['limit' => 200]);
$this->set(compact('product', 'types', 'attributes'));
}
这和我的亲戚有关。
产品表:
$this->belongsToMany('Attributes', [
'foreignKey' => 'product_id',
'targetForeignKey' => 'attribute_id',
'joinTable' => 'products_attributes'
]);
属性表:
$this->belongsToMany('Products', [
'foreignKey' => 'attribute_id',
'targetForeignKey' => 'product_id',
'joinTable' => 'products_attributes'
]);
这些是属性 `product_attributes` 数据库中有:
unique_iq INT,
product_id INT,
attribute_id INT,
amount INT,
value DOUBLE,
information VARCHAR(150)
节约策略解决不了我的问题。还是一样的结果。
1条答案
按热度按时间62lalag41#
只是一个解决办法,但它应该工作。等待一个更蛋糕的方式
因为你基本上想填补
products
以及product_attributes
表可以这样设置新关系产品表:
以这种方式塑造你的数据
这将在中创建新行
products
两排新的product_attributes