The*_*Hud 6 php annotations traits symfony doctrine-orm
我正在使用特征在 Symfony 应用程序中实现一些可标记的行为,使用 Doctrine2 进行持久化,并使用注释来配置它。
我的主要烦恼是在 trait 中,我的 IDE 不知道 $this->tags 的类型,并抛出一堆警告。我非常强迫症在这里记录我的代码,以便其他开发人员很容易上手。
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;
}
Run Code Online (Sandbox Code Playgroud)
据我所知,我的问题是我无法在特征中定义 $tags 属性,因为我需要覆盖注释。
我可以完全避免在 TaggableMethods 中定义 $tags,但对我来说,这破坏了封装,或者至少使代码更难阅读。
我可以使用 Yaml 或 XML 配置持久性,但我的所有其余实体都使用注释。
所以我正在寻找一种方法来避免生成运行时通知,Symfony 将其变成 ContextErrorException,从而在开发过程中杀死我的脚本。
这可能与“我们可以使用特征将 manyToOne 关系映射到 dotic2 吗? ”和“特征 - 与父类的属性冲突”有关
此外,“ PHP 5.4:为什么类可以覆盖具有不同签名的特征方法? ”中提到的继承方法的行为听起来非常接近我想要的属性 - 谁能解释为什么存在属性和方法之间的差异?
| 归档时间: |
|
| 查看次数: |
3699 次 |
| 最近记录: |