Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony 如何在控制器中向Shopware 6产品添加新的交叉销售?_Symfony_Criteria_Shopware - Fatal编程技术网

Symfony 如何在控制器中向Shopware 6产品添加新的交叉销售?

Symfony 如何在控制器中向Shopware 6产品添加新的交叉销售?,symfony,criteria,shopware,Symfony,Criteria,Shopware,我有一个与其他产品id链接的产品id字典。对于每个产品,我希望使用上述关系中的id创建新的交叉销售(产品列表)。 是否可以在控制器中更新产品交叉销售? 应该通过更新方法还是其他方法来完成 $this->productRepository->update([$productData], Context::createDefaultContext()); Update或Upsert似乎对产品也没有任何作用 $crossSellingData = [ 'id' => $c

我有一个与其他产品id链接的产品id字典。对于每个产品,我希望使用上述关系中的id创建新的交叉销售(产品列表)。 是否可以在控制器中更新产品交叉销售?
应该通过更新方法还是其他方法来完成

$this->productRepository->update([$productData], Context::createDefaultContext());
Update或Upsert似乎对产品也没有任何作用

 $crossSellingData = [
    'id' => $crossSellingIds[0],
    'name' => 'First Cross Selling',
    'position' => 1,
    'type' => 'productList',
    'active' => true,
    'assignedProducts' => [
        [
            'productId' => "5f5365e2adb446f6958faa4257c0af6d",
            'position' => 1
        ]
    ],
];
$productRepository->upsert([['id' => $productId, 'crossSelling' => $crossSellingData]], Context::createDefaultContext());

您可以使用
productCrossSellingRepository
(表:
product\u crossselling
)来执行此操作


还可以在产品存储库中添加交叉销售。 我在您的原始代码中看到两个问题:

  • 关联名为
    crossSellings
    ,您使用
    crossSelling
    ,因为没有名为
    crossSelling
    的字段,存储库会忽略该键,这就是更新无效的方式
  • 关联太多,这意味着存储库需要一个
    交叉销售列表,而不是一个交叉销售列表
  • 因此,原始upsert调用中的这些细微更改应该可以解决您的问题:

    $productRepository->upert([['id'=>$productId,'crossSellings'=>[$crossSellingData]],Context::createDefaultContext());
    

    注意交叉销售协会名称中的尾随
    s
    ,以及交叉销售数据周围的额外
    []
    数组这是正确的答案,我做错的是我试图更新产品而不是交叉销售。谢谢!
    $newCrossSelling = [
        'productId' => $mainProductId,
        'active' => true,
        'name' => 'First Cross Selling',
        'type' => 'productList',
        'position' => 1,
        'assignedProducts' => [
            [
                'productId' => $firstCrossSellingProductId,
                'position' => 1,
            ],
            [
                'productId' => $secondCrossSellingProductId,
                'position' => 2,
            ],
        ]
    ];
    
    $this->productCrossSellingRepository->create([$newCrossSelling], Context::createDefaultContext());