3

I have an entity Test which will get its properties (and basic methods) from traits:

class Test {
   use Trait_title;
}

trait Trait_title{
    /**
     * @ORM\Column(type="string", length=255, nullable=false)
     */
    private $title;
}

That works correctly. But when I try to put the annotations in the Test Class in front of the use statement, partially or complete they are just ignored by symfony when I try to update the schema:

class Test {
    /**
     * @ORM\Column(type="string", length=255, nullable=false) //will be ignored...
     */
   use Trait_title;
}

trait Trait_title {
    private $title;
}

The purpose of this is to move defaults for doctrine annotations into the trait, but to be allowed to set some custom annotations like nullable per entity as well.

musicman
  • 504
  • 6
  • 16
  • The hole context is being imported on the use statement, not just the propriety, I don't think you will get what you want from this approach. – Jean Carlo Machado Mar 27 '16 at 16:37

1 Answers1

3

What you are looking for is a mapping override.

You should look at the Doctrine official documentation to implement this: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/override-field-association-mappings-in-subclasses.html

There is also an example exactly for your use case, overriding a Trait mapping informations.

Sometimes also the mapping to override comes from entities using traits where the traits have mapping metadata. This tutorial explains how to override mapping metadata, i.e. attributes and associations metadata in particular.

The second approach would be to override the trait property by redefining the mapping informations. See this answer for more details about this solution: https://stackoverflow.com/a/11939306/4829152

Community
  • 1
  • 1
Remg
  • 176
  • 1
  • 5