多个相同的实体关系 [英] Multiple identical entitiy relation
问题描述
我有一个产品
,属性
和 product_attribute
表。
每个产品可以有多个属性
,这些关系到目前为止都存储在 product_attributes
中。
但是一个产品可以多次具有相同的属性。我只能将不同的属性链接到产品。
I've got a products
, attributes
, and product_attributes
table.
Each product can have multiple attributes
and these relations were stored in product_attributes
, so far so good.
But a product can have the same attribute multiple times. I just only can link different attributes to a product.
例如:
奥迪(产品)具有车轮(属性): Amount (连接数据)= 2;
值(joinData)= front;
Audi(product) has Wheel(attribute): Amount(joinData) = 2; Value(joinData) = "front";
和
Audi (产品)具有轮(属性):金额(joinData )= 2;
值(joinData)=后方;
Audi(product) has Wheel(attribute): Amount(joinData) = 2; Value(joinData) = "rear";
仅带有的属性轮> value 后部保存,前轮丢失。
Only the attribute wheel with value rear is saved the front wheels are lost.
控制器无错误。
这是 $ 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'
]
]
]
]
这是patchEntity之后的 $ product
的输出:
This is the output of $product
after patchEntity:
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添加函数:
ProductController Add function:
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
中具有的属性DB:
These are the attributes product_attributes
have in the DB:
unique_iq INT,
product_id INT,
attribute_id INT,
amount INT,
value DOUBLE,
information VARCHAR(150)
保存策略没有解决我的问题。仍然是相同的结果。
Save Strategy doesn't solve my problem. Still same result.
推荐答案
只是一种解决方法,但它应该可以工作。等待更多的蛋糕方式
Just a workaround but it should work. Waiting for a more cake way
因为您基本上想填充产品
和 product_attributes
表,您可以通过这种方式设置新的关系
Since you basically want to fill products
and product_attributes
tables you can set a new relationship this way
产品表:
$this->hasMany('ProductsAttributes', [ /* configure keys here */ ]);
用这种方式塑造数据
[
'type_id' => '12',
'name' => 'Audi',
'thumbnail' => '',
'image' => '',
'products_attributes' => [
[
'attribute_id' => '9',
'amount' => '2',
'value' => '1',
'information' => 'front'
],
[
'attribute_id' => '9',
'amount' => '2',
'value' => '1',
'information' => 'rear'
]
]
]
这将创建产品
中的新行和 product_attributes
这篇关于多个相同的实体关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!