gpt4 book ai didi

php - 多个相同的实体关系

转载 作者:行者123 更新时间:2023-11-29 05:54:42 25 4
gpt4 key购买 nike

我有一个productsattributesproduct_attributes 表。每个产品都可以有多个 attributes,这些关系存储在 product_attributes 中,到目前为止一切顺利。但是一个产品可以多次具有相同的属性。我只能将不同的属性链接到产品。

例如:

Audi(product) has Wheel(attribute): Amount(joinData) = 2; Value(joinData) = "front";

Audi(product) has Wheel(attribute): Amount(joinData) = 2; Value(joinData) = "rear";

只保存后轮的属性轮,前轮丢失。

Controller 没有错误。

这是 $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 的输出:

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)

保存策略不能解决我的问题。结果还是一样。

最佳答案

只是一种变通方法,但它应该有效。等待更多的蛋糕方式

因为您基本上想要填充productsproduct_attributes 表,所以您可以通过这种方式设置新的关系

产品表:

$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'
]
]
]

这将在 products 中创建一个新行,在 product_attributes 中创建两个新行

关于php - 多个相同的实体关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50761347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com