ibrows / association-resolver-bundle
解析注解中的关系
Requires
- php: >=5.3.0
- ibrows/annotation-reader: 1.*
- symfony/framework-bundle: >=2.1
This package is auto-updated.
Last update: 2024-09-15 19:34:22 UTC
README
此Bundle现在存储于此:https://gitlab.pwc-digital.ch/ec/bundles/IbrowsAssociationResolverBundle
iBrows Association Bundle
一个简单的Bundle,用于在实体从外部源导入时解析关联创建一个注解
见Annotation/OneToMany.php在这种情况下,我将为这个Bundle创建一个新的OneToMany注解。因此,我在'Annotation'文件夹中创建了一个名为OneToMany的新类,并用@Annotation注解该类,并从AbstractAssociation扩展它。如果您需要添加一些可以在注解中配置的属性,请在注解中创建一个公共属性,并创建getter/setter方法。
创建解析器
见Resolver/Type/OneToMany.php要处理带有我们的注解@OneToMany的实体,我们必须创建一个与注解类名完全相同的解析器。我们的解析器扩展了AbstractResolver以正确处理注解的实体。在扩展AbstractResolver时,我们必须实现一个抽象函数来处理实体并接收所有配置的属性。
    
        /**
         * @param ResultBag $resultBag
         * @param AssociationMappingInfoInterface $mappingInfo
         * @param string $propertyName
         * @param mixed $entity
         * @param OutputInterface $output
         * @return ResolverInterface
         */
        public function resolveAssociation(
            ResultBag $resultBag,
            AssociationMappingInfoInterface $mappingInfo,
            $propertyName,
            $entity,
            OutputInterface $output
        )
    
在函数体内,我们从mappinginfo中提取数据以进行进一步操作。在MappingInfo对象中有两个属性。一个是Annotation,另一个是Metadata。Annotation对象包含所有可以在注解中配置的数据。在我们的情况下,我们有两个属性
public $collectionAddFunctionName; public $collectionRemoveFunctionName;
collectionAddFunctionName用于自定义添加函数。如果没有设置此属性,方法名将以'add'为前缀,以属性名为后缀生成
collectionRemoveFunctionName用于自定义移除函数。如果没有设置此属性,方法名将以'remove'为前缀,以属性名为后缀生成
服务
要使解析器可用,您必须在service.xml中将解析器注册为服务<service id="ibrows_associationresolver.resolver.onetoone" class="Ibrows\AssociationResolver\Resolver\Type\OneToOne">
    <argument type="service" id="doctrine.orm.entity_manager" />
    <tag priority="-20" name="ibrows_associationresolver.resolverchain" />
    <call method="setSoftdeletable">
        <argument>%ibrows_associationresolver.softdelete%</argument>
    </call>
    <call method="setSoftdeletableGetter">
        <argument>%ibrows_associationresolver.softdeletegetter%</argument>
    </call>
</service>
´
该标签定义了链中的位置。