Can I override the property of PHP properties in a user class to use Doctrine2 annotations?

I use traits to implement some Taggable behavior in a Symfony application, using Doctrine2 to save and annotate to configure it.

The main annoyance that I have is that in this symptom my IDE has no idea about the type of $ this-> tags and generates a bunch of warnings. I have pretty OCD about having my code registered here, so it is very easy for other developers to pick up.

trait TaggableMethods { /** @var \Doctrine\Common\Collections\Collection */ protected $tags; // <-- Can't seem to define hereโ€ฆ public function addTag(Tag $tag) { $this->tags->add($tag); } public function removeTag(Tag $tag) { $this->tags->removeElement($tag); } public function getTags() { return $this->tags; } } class TaggableThingA { use TaggableMethods; /** * @var \Doctrine\Common\Collections\Collection * @ORM\ManyToMany(targetEntity="Tag") * @ORM\JoinTable(name="ThingA__Tag") */ protected $tags; // <--โ€ฆ because PHP complains about this } class TaggableThingB { use TaggableMethods; /** * @var \Doctrine\Common\Collections\Collection * @ORM\ManyToMany(targetEntity="Tag") * @ORM\JoinTable(name="ThingB__Tag") */ protected $tags; } 

My problem, as far as I can tell, is that I cannot define the $ tags property in the property, because I need to override the annotations.

I can avoid defining $ tags altogether in TagGableMethods methods, but for me it breaks the encapsulation or at least makes the code a little harder to read.

I can set up persistence using either Jam or XML, but all of my other objects use annotations.

So, I am looking for a way to avoid the start notification that is generated that Symfony turns into a ContextErrorException, killing my script at design time.

This is probably due to the fact that we can use traits to map the relationship of manyToOne to doctrine2? and Traits - property conflict with parent class "

Also, the behavior for the inherited methods mentioned in โ€œ PHP 5.4: why do classes override trait methods with a different signature? โ€ Sounds very close to what I want for a property - can anyone explain why there is a difference between properties and methods ?

+4
source share
2 answers

You cannot override a characteristic, but you can rename it.

An example of this is here!

https://github.com/slimphp/Slim/blob/3.x/Slim/App.php#L47-L50

0
source

Late, but it might help someone. You can use associations or attributes ( @AssociationOverrides , @AttributeOverrides ) as described in section 6.4. Replaces .

0
source

All Articles