1 yaml doctrine one-to-many doctrine-orm
我试图用以下关系制作3个实体(Item,Agree,Disagree).
但是只有一个关系(后来宣布)中有两个关系.
这是我的.yml文件.
Entities\Item:
type: entity
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
oneToMany:
agrees:
targetEntity: Agree
mappedBy: items
oneToMany:
disagrees:
targetEntity: Disagree
mappedBy: items
Run Code Online (Sandbox Code Playgroud)
Entities\Agree:
type: entity
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
manyToOne:
items:
targetEntity: Item
inversedBy: agrees
Run Code Online (Sandbox Code Playgroud)
Entities\Disagree:
type: entity
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
manyToOne:
items:
targetEntity: Item
inversedBy: disagrees
Run Code Online (Sandbox Code Playgroud)
下面的代码是Doctrine2自动生成的Item.php.如您所见,它根本不包含"同意".
namespace Entities;
class Item {
private $id;
private $disagrees;
public function __construct() {
$this->disagrees = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getId() {
return $this->id;
}
public function addDisagrees(\Entities\Disagree $disagrees) {
$this->disagrees[] = $disagrees;
}
public function getDisagrees() {
return $this->disagrees;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我切换声明顺序(首先是"不同意",然后是"同意",如下所示),则Item.php此时只有"同意"相关的代码.
Entities\Item:
type: entity
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
oneToMany:
disagrees:
targetEntity: Disagree
mappedBy: items
oneToMany:
agrees:
targetEntity: Agree
mappedBy: items
Run Code Online (Sandbox Code Playgroud)
我的代码出了什么问题?任何评论都会有所帮助.
项目,同意和不同意只是展示此问题的示例.在实际项目中,同意和不同意是完全不同的实体.所以,不要建议我将它们合并到统一的实体中.:)
你很接近,你只需要在相同的类型声明下放置所有相同的关联映射类型:)
oneToMany:
agrees:
targetEntity: Agree
mappedBy: items
disagrees:
targetEntity: Disagree
mappedBy: items
Run Code Online (Sandbox Code Playgroud)