php - 自定义实体与扩展实体一对一关联

标签 php shopware shopware6

我正在编写一个 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>

现在有了这段代码,shopware 完全崩溃了,我在大多数端点上都收到了 HTTP 错误 503。我认为它会遇到递归,因为如果我在客户扩展中将 OneToOneAssociationField 自动加载属性设置为 false,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 自动加载设置回 true 并在我的自定义实体中设置为 false 一切似乎都有效,但这不是很实用。

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

最佳答案

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

没有更多的无限循环。 我猜这两个类试图相互加载,但类的实例总是不同的。不会确切知道。 我在 shopware 代码中看到有一些使用 OneToOneAssociationField 的示例。 这就是我不在两个对象中自动加载的原因。 到目前为止,我不知道这个无限循环是错误还是未使用代码功能。

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

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

将在 product 表中创建一列 myLoremIpsum。 我必须将表 product 中字段 id 的值放入。 我看到了安装这个例子的值(value): 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-bundle

最好有官方文档和一个带有 OneToOneAssociationField 的例子。

关于php - 自定义实体与扩展实体一对一关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63410315/

相关文章:

php - 表单字段为空然后从数据库中检索到的输入值为 0

php - Plivo 租一个号码 Api 不适合我

php - Shopware 6 价格收集器/处理器的折扣计算错误

javascript - 寻找一种好方法来获取 ExtJS 类样式组件中事件处理程序内部的引用

shopware - 使用购物车促销时出现 OOM 和超时

php - 如何使用 PHP DOMDocument 删除父 Div

php - 了解 PHP 和 MySQL 安全性基础知识

events - 如何向 Shopware 中的帐户 Controller 添加操作

商店用品 6 : Extend existing core DAL entities with a reference

shopware - 重新发送特定订单状态电子邮件,例如订单交付在 Shopware 6 中发货