You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shopware 6 数据库写入数据时类型错误问题求助

Shopware 6 数据写入问题

我按照官方文档尝试向数据表写入数据,但始终无法解决问题。当前情况:

  • 写入时提示数据类型必须为数组,但传入数组后会自动转为多个INT值;
  • 尝试循环逐个插入字符串值,问题依旧。

相关代码片段

/src/Service/ReadingAndWritingData.php

class ReadingAndWritingData
{
    /** @var EntityRepository $productRepository */
    private EntityRepository $productRepository;

    private EntityRepository $deepLearningRepository;

    public function __construct(EntityRepository $productRepository,EntityRepository $deepLearningRepository)
    {
        $this->productRepository = $productRepository;
        $this->deepLearningRepository = $deepLearningRepository;
    }

    public function writeData(Context $context): void
    {
        $productId = Uuid::randomHex();
        $date = new \DateTime();
        $createdAt = $date->format('Y-m-d H:i:s');

        $productIds = $this->getProductIds($context);

            $this->deepLearningRepository->create([
                [
                    'id' => $productId,
                    'product_id' => $productIds,
                    'created_at' =>$createdAt
                ]
            ], $context);

    }

    private function getProductIds(Context $context): array
    {
        $criteria = new Criteria();

        $criteria->addFilter(new EqualsFilter('active', 1));

        return $this->productRepository->searchIds($criteria, $context)->getIds();
    }

}

/src/Core/Content/DeepLearning/DeepLearningDefinition.php

protected function defineFields(): FieldCollection
{
    return new FieldCollection([
        (new IdField('id', 'id'))->addFlags(new Required(), new PrimaryKey()),
        (new FkField('product_id', 'productId', ProductDefinition::class)),

        new OneToOneAssociationField('product_id', 'product_id', 'id', ProductDefinition::class, false)

    ]);
}

/src/DeepLearning.php

public function activate(ActivateContext $activateContext): void
{
    $this->getCustomFieldsInstaller()->writeData($activateContext->getContext());
}

private function getCustomFieldsInstaller(): ReadingAndWritingData
{
    if ($this->container->has(ReadingAndWritingData::class)) {
        return $this->container->get(ReadingAndWritingData::class);
    }

    return new ReadingAndWritingData(
        $this->container->get('product.repository'),
        $this->container->get('deep_learning.repository')
    );
}

问题根源与解决办法

  1. 关联关系定义冲突
    你在实体定义中使用了OneToOneAssociationField,但关联字段名和外键字段名都是product_id,这会导致字段映射混乱。一对一关联要求product_id是单个UUID值,不能传入数组。

    修正实体定义,将关联字段名改为product,避免和外键字段重复:

    protected function defineFields(): FieldCollection
    {
        return new FieldCollection([
            (new IdField('id', 'id'))->addFlags(new Required(), new PrimaryKey()),
            (new FkField('product_id', 'productId', ProductDefinition::class)),
            // 修改关联字段名,避免与外键字段冲突
            new OneToOneAssociationField('product', 'product_id', 'id', ProductDefinition::class, false)
        ]);
    }
    
  2. 数据写入逻辑错误
    getProductIds()返回的是多个产品ID的数组,但一对一关联的product_id只能接收单个值。如果要为每个活跃产品创建一条DeepLearning记录,需要循环数组逐个插入:

    修改writeData方法:

    public function writeData(Context $context): void
    {
        $date = new \DateTime();
        $createdAt = $date->format('Y-m-d H:i:s');
    
        $productIds = $this->getProductIds($context);
    
        // 循环每个产品ID,创建对应记录
        foreach ($productIds as $productId) {
            $deepLearningId = Uuid::randomHex();
            $this->deepLearningRepository->create([
                [
                    'id' => $deepLearningId,
                    'product_id' => $productId,
                    'created_at' => $createdAt
                ]
            ], $context);
        }
    }
    
  3. 若需关联多个产品
    如果你的业务场景是一条DeepLearning记录对应多个产品,那不能用一对一关联,需要改为多对多关联:

    • 创建中间表关联deep_learning和product;
    • 在DeepLearningDefinition中定义ManyToManyAssociationField;
    • 写入数据时通过关联字段传入产品ID数组。

内容的提问来源于stack exchange,提问作者Shad0w

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 17:25:30