问题描述
我正在编写一个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',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。我认为这会遇到递归问题,因为如果我在客户扩展中将OnetoOneAssociationField autoload属性设置为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->oAuthUser
是null
。
如果我交换了它,请将扩展名上的OnetoOneAssociationField自动加载设置回true
并在我的自定义实体中设置为false
,一切似乎都可以正常工作,但这不是很实用。
对我而言,递归部分似乎是商店软件中的错误,还是我这边有错误?我需要以某种方式提供自定义结构吗?
解决方法
我有同样的问题。 在产品实体上添加了扩展名。 在我的自定义定义类(例如OAuthUserDefinition)中,我将autoloading设置为false。 然后,我在扩展程序类(例如CustomerExtension)中将自动加载设置为true。
没有更多的无限循环。
我猜这两个类正在尝试互相加载,但是这些类的实例总是其他的。不会确切知道。
我在商店软件代码中看到有一些使用OneToOneAssociationField
的示例。
这就是为什么我不在两个对象中自动加载的原因。
到目前为止,我不知道这个无限循环是错误还是错过了使用代码功能。
在编辑产品页面上,管理员仍然出现错误。 唯一可行的方法是用所有产品的NULL /空值填充我的表。在迁移脚本中也有类似的内容:
$this->updateInheritance($connection,'product','myLoremIpsum');
将在myLoremIpsum
表中创建列product
。
我不得不从表id
的字段product
中放入值。
通过安装此示例,我看到了什么价值:
https://github.com/shopware/swag-docs-bundle-example
很高兴拥有正式文档和带有OneToOneAssociationField
的示例。
您是否在CustomerExtension-Class中定义了getDefinitionClass-Function?
public function getDefinitionClass(): string
{
return CustomerDefinition::class;
}
您还可以分享您的数据库迁移吗?