我尝试使用Doctrine 2 + Symfony创建这三个类,并使用双向YAML映射。
一切正常,没有错误,但生成的实体仅包含User实体的两个第一个集合声明。该实体也没有缺失链接的添加,删除和获取功能。
我尝试分别生成每个映射,工作正常。
这是Doctrine2的限制吗?
生成的MyBundle \ Entity \ User.php构造函数(缺少Token类的ArrayCollection):
public function __construct()
{
$this->owned = new \Doctrine\Common\Collections\ArrayCollection();
$this->shared = new \Doctrine\Common\Collections\ArrayCollection();
}
Collection.orm.yml:
MyBundle\Entity\Collection:
type: entity
table: collection
id:
collection_id:
type: integer
generator:
strategy: AUTO
fields:
...
manyToOne:
owner:
targetEntity: User
inversedBy: owned
joinColumn:
name: user_id
referencedColumnName: user_id
manyToMany:
shares:
targetEntity: User
inversedBy: shared
joinTable:
name: shares
joinColumns:
collection_id:
referencedColumnName: collection_id
inverseJoinColumns:
user_id:
referencedColumnName: user_id
Token.orm.yml:
MyBundle\Entity\Token:
type: entity
table: token
id:
token_id:
type: integer
generator:
strategy: AUTO
fields:
...
manyToOne:
user:
targetEntity: User
inversedBy: tokens
joinColumn:
name: user_id
referencedColumnName: user_id
User.orm.yml:
MyBundle\Entity\User:
type: entity
table: user
id:
user_id:
type: integer
generator:
strategy: AUTO
fields:
...
oneToMany:
owned:
targetEntity: Collection
mappedBy: owner
oneToMany:
tokens:
targetEntity: Token
mappedBy: user
manyToMany:
shared:
targetEntity: Collection
mappedBy: shares
答案 0 :(得分:1)
Yaml不处理重复的密钥。 oneToMany
应定义一次。
MyBundle\Entity\User:
type: entity
table: user
id:
user_id:
type: integer
generator:
strategy: AUTO
fields:
...
oneToMany:
owned:
targetEntity: Collection
mappedBy: owner
tokens:
targetEntity: Token
mappedBy: user
manyToMany:
shared:
targetEntity: Collection
mappedBy: shares