问题描述
- 项目一对多,我试图使3个实体(项目,同意,不同意)同意
- 项目一对多不同意
但只有一个关系(稍后声明)
Here're我的.yml文件。
Entities\Item:
type:entity
fields:
id:
type:integer
id :true
生成器:
策略:AUTO
oneToMany:
同意:
targetEntity:Agree
mappedBy:items
oneToMany:
不同意:
targetEntity:不同意
mappedBy:items
实体\Agree:
类型:实体
字段:
id:
类型:整数
id:true
生成器:
策略:AUTO
manyToOne:
items:
targetEntity:Item
inversedBy:agrees
Entities\Disagree:
type:entity
fields:
id:
type:integer
id:true
生成器:
策略:AUTO
manyToOne:
items:
targetEntity:Item
inversedBy:disagrees
下面的代码是由Doctrine2自动生成的Item.php。您可以看到,它不包含同意。
命名空间实体;
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;
}
}
如果我转换声明顺序(不同意,后跟Agree,如下所示),Item.php此时只有同意相关的代码。
实体\Item:
类型:实体
字段:
id:
类型:整数
id:true
生成器:
策略:AUTO
oneToMany:
不同意:
targetEntity:不同意
mappedBy:items
oneToMany:
同意:
targetEntity:Agree
mappedBy:items
我的代码有什么问题?任何意见都将有所帮助。
项目,同意和不同意只是显示此问题的示例。在实际项目中,同意和不同意是完全不同的实体。所以,不要建议我把它们合并成统一的实体。 :)
你很亲密,只需要把所有相同的关联映射类型放在相同的类型声明下:) p>
oneToMany:
同意:
targetEntity:Agree
mappedBy:items
不同意:
targetEntity:不同意
mappedBy:items
I'm trying to make 3 entities (Item, Agree, Disagree) with following relations.
- Item one-to-many Agree
- Item one-to-many Disagree
But only one relation (declared later) out of two has made.
Here're my .yml files.
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
Entities\Agree:
type: entity
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
manyToOne:
items:
targetEntity: Item
inversedBy: agrees
Entities\Disagree:
type: entity
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
manyToOne:
items:
targetEntity: Item
inversedBy: disagrees
And the code below is the Item.php auto-generated by Doctrine2. As you can see, it doesn't contains 'Agree' at all.
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;
}
}
If I switches the declaration order ('Disagree' first, followed by'Agree', like the below), Item.php has only 'Agree'-related code in this time.
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
What's wrong with my code? Any comments will be helpful.
Item, Agree and Disagree are just samples to show this problem. In real project, Agree and Disagrees are totally different entity. So, don't suggest me to merge them into unified entity. :)
You were close, you just need to put all same association mapping types under the same type declaration :)
oneToMany:
agrees:
targetEntity: Agree
mappedBy: items
disagrees:
targetEntity: Disagree
mappedBy: items
这篇关于教义2不能使“2”一对多的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!