如何为Shopware 6现有实体添加缺失的Getter和Setter(B2Bsellers套件)
扩展EmployeePermissionTranslationEntity并添加自定义Getter
下面是几种可行的实现方式,根据你的实际场景选择:
方式一:用Trait注入Getter (推荐)
- 在自定义插件里创建一个Trait,写好需要的Getter方法:
<?php namespace YourCustomPlugin\Component\Entity\Trait; trait EmployeePermissionTranslationExtensionTrait { // 替换成你需要的字段名和返回类型 public function getCustomPermissionLabel(): ?string { return $this->customPermissionLabel; } // 按需添加Setter public function setCustomPermissionLabel(?string $label): void { $this->customPermissionLabel = $label; } }
- 创建Compiler Pass来把Trait注入原实体:
<?php namespace YourCustomPlugin\Framework\CompilerPass; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use YourCustomPlugin\Component\Entity\Trait\EmployeePermissionTranslationExtensionTrait; class EmployeePermissionTranslationEntityPass implements CompilerPassInterface { public function process(ContainerBuilder $container): void { // 替换成B2Bsellers插件中EmployeePermissionTranslationEntity的完整类名 $targetEntity = 'B2Bsellers\B2BSellersPlugin\Entity\EmployeePermissionTranslationEntity'; // 通过Shopware的实体扩展加载器注入Trait $container->getDefinition('shopware.entity.extension_loader') ->addMethodCall('addExtension', [ $targetEntity, 'custom_permission_extension', EmployeePermissionTranslationExtensionTrait::class ]); } }
- 在插件主类中注册这个Compiler Pass:
<?php namespace YourCustomPlugin; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; use YourCustomPlugin\Framework\CompilerPass\EmployeePermissionTranslationEntityPass; class YourCustomPlugin extends Bundle { public function build(ContainerBuilder $container): void { parent::build($container); $container->addCompilerPass(new EmployeePermissionTranslationEntityPass()); } }
方式二:创建实体装饰器
如果原实体是通过服务容器实例化的,可以用装饰器扩展:
- 写一个继承自原实体的装饰类,添加Getter:
<?php namespace YourCustomPlugin\Entity; use B2Bsellers\B2BSellersPlugin\Entity\EmployeePermissionTranslationEntity; class EmployeePermissionTranslationDecorator extends EmployeePermissionTranslationEntity { public function getCustomPermissionLabel(): ?string { return $this->customPermissionLabel; } }
- 在插件的
src/Resources/config/services.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="YourCustomPlugin\Entity\EmployeePermissionTranslationDecorator" decorates="B2Bsellers\B2BSellersPlugin\Entity\EmployeePermissionTranslationEntity"> <argument type="service" id="YourCustomPlugin\Entity\EmployeePermissionTranslationDecorator.inner"/> </service> </services> </container>
方式三:临时用反射取值 (不推荐长期使用)
如果只是临时调试或快速获取值,可以用反射绕过无Getter的限制:
// $entity 是你的EmployeePermissionTranslationEntity实例 $reflectionProp = new \ReflectionProperty($entity, 'customPermissionLabel'); $reflectionProp->setAccessible(true); $fieldValue = $reflectionProp->getValue($entity);
内容的提问来源于stack exchange,提问作者smileyrasmus
相关产品推荐
相关产品推荐

