我刚开始使用Codeigniter的Doctrine。
我有一个名为Material的实体,与另一个名为elementCombination的实体有关系。
要检索所有材料,我使用此代码
$query = $this->doctrine->em->getRepository('Entities\Material')->createQueryBuilder('m');
$materials = $query->getQuery()->getResult();
当我使用getCombination()函数查找每个材质的元素(ElementCombination实体)时,我总是得到所有“材质”的相同结果,实际上它看起来像是合并结果,因为一个材质可能比其他材质具有更多元素。对我来说这很奇怪。
我通过这样做“修复”了这个,重新查询或刷新了每种材料的组合:
foreach ($materials as $key => $material)
{
$this->doctrine->em->flush();
$this->doctrine->em->clear();
//$m_combination_1 is a ArrayCollection of ElementCombination
$m_combination_1= $this->doctrine->em->getRepository('Entities\ElementCombination')->findBy(array('material' => $material->getId()));
$material->setCombination($m_combination_1);
}
我觉得这不是一个很好的方法,所以,我想知道这种行为是否正常?我做错了什么?
yml实体是这样的。
材料
Entities\Material:
type: entity
table: materials
oneToMany:
similars:
targetEntity: Similar
mappedBy: parent
combination:
targetEntity: ElementCombination
mappedBy: material
.....
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
.....
和ElementCombination
Entities\ElementCombination:
type: entity
table: element_combinations
manyToOne:
material:
targetEntity: Material
inversedBy: combination
fields:
symbol:
type: string
length: 3
nullable: false
id: true
maxValue:
type: decimal
precision: 6
nullable: false
column: max_value
minValue:
type: decimal
precision: 6
nullable: false
column: min_value
avgValue:
type: decimal
precision: 6
nullable: false
column: avg_value
提前致谢。
修改
我发现解决方案(我知道)架构是错误的,我已经改变了这个,我可以摆脱代码中的很多东西
Entities\Material:
type: entity
table: materials
oneToMany:
similars:
targetEntity: Similar
mappedBy: parent
manyToMany:
combination:
targetEntity: ElementCombination
inversedBy: materials
cascade: {"remove" , "persist"}
joinTable:
name: material_element_combinations
joinColumns:
material_id:
referencedColumnName: id
inverseJoinColumns:
element_combination_id:
referencedColumnName : id
uniqueConstraints:
material_index:
columns:
- reference
- inner_name
和元素组合
Entities\ElementCombination:
type: entity
table: element_combinations
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
symbol:
type: string
length: 3
nullable: false
maxValue:
type: decimal
precision: 6
nullable: false
column: max_value
minValue:
type: decimal
precision: 6
nullable: false
column: min_value
avgValue:
type: decimal
precision: 6
nullable: false
column: avg_value
此致