首页 > 解决方案 > 多个相同实体关系

问题描述

我有一个products,attributesproduct_attributes表。每个产品可以有多个attributes并且这些关系被存储在 中product_attributes,到目前为止还不错。但是一个产品可以多次具有相同的属性。我只能将不同的属性链接到产品。

例如:

奥迪(产品)有Wheel(属性):Amount (joinData) = 2; (joinData)=“前”;

奥迪(产品)有Wheel(属性):Amount (joinData) = 2; (joinData)=“后”;

只有带有的属性轮被保存,前轮丢失。

控制器中没有错误。

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

这是$productpatchEntity 之后的输出:

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)

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

标签: phpmysqlcakephpcakephp-3.0

解决方案


只是一种解决方法,但它应该可以工作。等待更多的蛋糕方式

由于您基本上想要填写表格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


推荐阅读