Resolve relations over annotations

1.0.6 2014-08-18 17:07 UTC

This package is auto-updated.

Last update: 2024-04-15 18:40:22 UTC


This Bundle is now stored here: https://gitlab.pwc-digital.ch/ec/bundles/IbrowsAssociationResolverBundle

iBrows Association Bundle

A simple bundle to resolve associations on entities when they'll be imported from an external source

Create an annotation

See Annotation/OneToMany.php

In this case I'm going to create a new OneToMany-annotation for this bundle. Therefore I create a new class called OneToMany in the 'Annotation' folder and annotate the class with @Annotation and extend it from the AbstractAssociation If you need to add some properties which can be configured in the annotation create a public property in the Annotation and create the getter/setter

Create the resolver

See Resolver/Type/OneToMany.php

To handle the entity with our annotation @OneToMany we have to create a resolver with the exact same name as the annotation class name. Our resolver extends the AbstractResolver to handle the annotated entity correctly. While extending the AbstractResolver we have to implement an abstract function to handle an entity and recive all the configurated properties.

         * @param ResultBag $resultBag
         * @param AssociationMappingInfoInterface $mappingInfo
         * @param string $propertyName
         * @param mixed $entity
         * @param OutputInterface $output
         * @return ResolverInterface
        public function resolveAssociation(
            ResultBag $resultBag,
            AssociationMappingInfoInterface $mappingInfo,
            OutputInterface $output

in the function body we extract the data from the mappinginfo for further operations. Inside the MappingInfo object where two properties. The Annotation and the Metadata. The Annotation object holds all the data which can be configured in the annotation. In our case we have 2 properties

public $collectionAddFunctionName;
public $collectionRemoveFunctionName;

collectionAddFunctionName to customize the add function. If this property is not set the method name will be generated with an 'add' as prefix and the propertyname as suffix

collectionRemoveFunctionName to customize the remove function. If this property is not set the method name will be generated with an 'remove' as prefix and the propertyname as suffix

The Service

To make the resolver functional you have to register the resolver as a service in the 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">
    <call method="setSoftdeletableGetter">

The tag defines the position in the chain.