Skip to content

Latest commit

 

History

History
185 lines (148 loc) · 4.13 KB

yaml-mapping.md

File metadata and controls

185 lines (148 loc) · 4.13 KB

#YAML Mapping

Contents

Change Type Of Your Entities Relationship

If you have two mapped superclasses with a unidirectional relationship and you want to create entities with a bidirectional relationship based on those superclasses, you need to use the associationPropertyOverrides tag.

    namespace Acme\Bundle\DemoBundle\Entity;

    /**
     * Acme\Bundle\DemoBundle\Entity\BaseUser
     */
    abstract class BaseUser
    {
        /**
         * @var integer
         */
        protected $id;

        /**
         * @var BaseUserProfile
         */
         protected $profile;

         //...
    }
    Acme\Bundle\DemoBundle\Entity\BaseUser:
      type: mappedSuperclass
      id:
        id:
          type: integer
          generator:
            strategy: AUTO
      oneToOne:
        profile:
          targetEntity: Acme\Bundle\DemoBundle\Entity\BaseUserProfile
          joinColumn:
            name: profile_id
            referencedColumnName: id
    namespace Acme\Bundle\DemoBundle\Entity;

    /**
     * Acme\Bundle\DemoBundle\Entity\BaseUserProfile
     */
    abstract class BaseUserProfile
    {
        /**
         * @var integer
         */
        protected $id;

        //...
    }
    Acme\Bundle\DemoBundle\Entity\BaseUserProfile:
      type: mappedSuperclass
      id:
        id:
          type: integer
          generator:
            strategy: AUTO

Let's call their children User and UserProfile.

    namespace Acme\Bundle\DemoBundle\Entity;

    /**
     * Acme\Bundle\DemoBundle\Entity\User
     */
    class User
    {
         //...
    }
    Acme\Bundle\DemoBundle\Entity\User:
      type: entity
      table: user
      associationPropertyOverrides:
        profile:
          inversedBy: user
    namespace Acme\Bundle\DemoBundle\Entity;

    /**
     * Acme\Bundle\DemoBundle\Entity\UserProfile
     */
    class UserProfile
    {
         /**
          * @var User
          */
          protected $user;

          //...
    }
    Acme\Bundle\DemoBundle\Entity\UserProfile:
      type: entity
      table: user_profile
      associationPropertyOverrides:
        user:
          mappedBy: profile

After adding the associationPropertyOverrides tag to your children entities, the relationship between them becomes bidirectional.

Add/Remove Orphan Removal Strategy.

If you want to add or remove the orphanRemoval strategy to your relations, you need to use the orphanRemoval field of the associationPropertyOverride tag.

    namespace Acme\Bundle\DemoBundle\Entity;

    /**
     * Acme\Bundle\DemoBundle\Entity\User
     */
    class User
    {
         //...
    }
    Acme\Bundle\DemoBundle\Entity\User:
      type: entity
      table: user
      associationPropertyOverrides:
        profile:
          orphanRemoval: true

And that's it. Now orphaned UserProfile entities will be automatically removed after removing the link to them from the User entity.

Override Id Generation Strategy

If you want to override the generation strategy in your child entity, you need to use the idGenerationStrategyOverride annotation.

    namespace Acme\Bundle\DemoBundle\Entity;

    /**
     * Acme\Bundle\DemoBundle\Entity\User
     */
    class User
    {
         //...
    }
    Acme\Bundle\DemoBundle\Entity\User:
      type: entity
      idGenerationStrategyOverride: SEQUENCE

As a value for the strategy property, you can use the name from one of the doctrine strategies or a path to your custom strategy. Please remember that your custom strategy class should be inherited from Doctrine\ORM\Id\AbstractIdGenerator.


Index