1

私はこれをSymfony2とDoctrineで動作させることに固執しています。状況:ピアス情報(一般情報と世話情報)のあるページ。

ケアテイクは、それが適用される複数のピアスを持つことができ、ピアスは複数のケアテイクを持つことができます

データベースレイアウト:

Piercings:
    id
    name
    ...

Caretaking:
    id
    title
    description

piercing_to_caretaking
    id
    piercing_id
    caretaking_id

では、エンティティと対応するQuery / Dqlをどのように作成しますか?

4

1 に答える 1

3

ymlを使用してエンティティを定義している場合:

Piercing.orm.ymlに以下を追加します。

manyToMany:
    caretakings:
        targetEntity: Caretaking
        inversedBy: piercings
        joinTable:
            name: piercing_caretaking
            joinColumns:
                caretaking:
                    referencedColumnName: id
            inverseJoinColumns:
                piercing:
                    referencedColumnName: id

Caretaking.orm.ymlに次を追加します。

manyToMany:
    piercings:
      targetEntity: Piercing
      mappedBy: caretakings

通常の方法でエンティティを生成/更新します。

app/console doctrine:schema:update --dump-sql (to check results)
app/console doctrine:schema:update --force (to apply changes)

次に、ピアスまたはケアテイキングエンティティがある場合、次のような関連エンティティにアクセスできます。

$piercing->addCaretaking($caretaking); 
$caretakings = $piercing->getCaretakings();
...
$piercings = $caretaking->getPiercings();

注釈を使用してこれを行う方法などの詳細については、「Doctrineドキュメントのセクション5アソシエーションマッピング」のサブセクション5.1.4多対多双方向を参照してください。

于 2012-05-30T10:44:04.513 に答える