3

我正在编写一个 shopware 6 插件,它添加一个自定义实体并通过与现有实体的一对一关联将其关联。

这是我的自定义实体的重要部分:

class OAuthUserDefinition extends EntityDefinition
{
  [...]
  protected function defineFields(): FieldCollection {
    return new FieldCollection([
       (new IdField('id', 'id'))->addFlags(new Required(), new PrimaryKey()),
       (new FkField('customer_id', 'customerId', CustomerDefinition::class)),
       new OneToOneAssociationField('customer', 'customer_id', 'id', CustomerDefinition::class)
     ]);
   }
}

我像这样扩展了客户实体:

class CustomerExtension extends EntityExtension{
  public function extendFields(FieldCollection $collection): void {
    $collection->add(
      (new OneToOneAssociationField(
        'oAuthUser',
        'id',
        'customer_id',
        OAuthUserDefinition::class
       ))
     );
  }
  
  public function getDefinitionClass(): string
  {
     return CustomerDefinition::class;
  }
}

这是我的迁移:

    public function update(Connection $connection): void
    {
        $connection->executeUpdate('
            CREATE TABLE IF NOT EXISTS `nt_oauth_user` (
              `id` BINARY(16) NOT NULL,
              `customer_id` BINARY(16) NULL,
              `oauth_user_id` VARCHAR(36) NOT NULL,
              `created_at` DATETIME(3) NOT NULL,
              `updated_at` DATETIME(3) NULL,
              PRIMARY KEY (`id`),
              CONSTRAINT `fk.oauth_user.customer_id` FOREIGN KEY (`customer_id`)
                REFERENCES `customer` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
        ');
    }

还有我的 service.xml

<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>

        <service id="Nt\OAuthClient\Core\System\OAuthUser\OAuthUserDefinition">
            <tag name="shopware.entity.definition" entity="nt_oauth_user" />
        </service>


        <service id="Nt\OAuthClient\Core\Checkout\Customer\CustomerExtension">
            <tag name="shopware.entity.extension"/>
        </service>

    </services>
</container>

现在有了这个代码,商店软件完全崩溃了,我在大多数端点上都收到了 HTTP 错误 503。我认为它会遇到递归,因为如果我false在客户扩展中将 OneToOneAssociationField 自动加载属性设置为 503 错误就会消失。但是store-api/v1/account/customer给了我一个

"status": "500",
"title": "Internal Server Error",
"detail": "Argument 1 passed to Shopware\\Core\\System\\SalesChannel\\Api\\StructEncoder::encode() must be an instance of Shopware\\Core\\Framework\\Struct\\Struct, null given, called in /app/vendor/shopware/platform/src/Core/System/SalesChannel/Api/StructEncoder.php on line 214",

因为customer->extensions->oAuthUsernull

如果我交换它,请将 OneToOneAssociationField 扩展上的自动加载设置回我truefalse自定义实体中,一切似乎都可以正常工作,但这不是很实用。

它的递归部分对我来说似乎是商店用品中的错误,还是我这边有错误?我是否需要以某种方式提供自定义结构?

4

2 回答 2

2

我遇到过同样的问题。在产品实体上添加了扩展。在我的自定义定义类(例如 OAuthUserDefinition)中,我将自动加载设置为 false。我在扩展类(例如CustomerExtension)中将自动加载设置为true。

没有更多的无限循环。我猜这两个类试图相互加载,但类的实例总是其他的。具体不会知道。我在商店软件代码中看到有几个使用OneToOneAssociationField. 这就是为什么我不在两个对象中自动加载的原因。到目前为止,我不知道这个无限循环是错误还是错过了代码功能的使用。

我在编辑产品页面上仍然遇到管理员错误。唯一可行的方法是用所有产品的 NULL/空值填充我的表。在迁移脚本中也有类似的东西:

$this->updateInheritance($connection, 'product', 'myLoremIpsum');

myLoremIpsum将在表中创建一列productid我必须从表中的字段中输入值product。我通过安装这个例子看到了什么价值: https ://github.com/shopware/swag-docs-bundle-example

https://docs.shopware.com/en/shopware-platform-dev-en/how-to/indepth-guide-bundle/introduction?category=shopware-platform-dev-en/how-to/indepth-guide-捆

如果有官方文档和OneToOneAssociationField.

于 2020-09-01T07:22:23.380 回答
0

您是否在 CustomerExtension-Class 中定义了 getDefinitionClass-Function?

public function getDefinitionClass(): string
{
    return CustomerDefinition::class;
}

您也可以分享您的数据库迁移吗?

于 2020-08-21T09:12:41.210 回答