Codeigniter を使用して Doctrine を使い始めたばかりです。
私は、elementCombination と呼ばれる別のエンティティとの関係 onetomany を持つ Material と呼ばれるエンティティを持っています。
すべての資料を取得するには、このコードを使用します
$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
前もって感謝します。
編集:
私は解決策を見つけました(私はtnink)スキーマが間違っています、私はこれに変更しました、そして私はコード内の多くのものを取り除くことができました
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
よろしく