1

私はそこに何人かの仲間の教義ユーザーがいることを望んでいます。
これが私の関係の単純化されたYAMLです:

Collection:
  columns:
    id:           { type: integer(4), notnull: true, primary: true, autoincrement: true }
    name:         { type: string(255), notnull: true, unique: true }
  relations:
    Items:
      class: Item
      refClass: CollectionItem
      foreignAlias: Collections
      type: many
      foreignType: many

Item:
  columns:
    id:   { type: integer(4), notnull: true, primary: true, autoincrement: true }
    name: { type: string(255), notnull: true }

CollectionItem:
  columns:
    id:       { type: integer(4), notnull: true, primary: true, autoincrement: true }
    collection_id:  { type: integer(4) }
    item_id:  { type: integer(4) }
  relations:
    Collection:
      foreignAlias: CollectionItem
      foreignType: one
    Item:
      foreignAlias: CollectionItem
      foreignType: one

コレクションが同じアイテムの多くのコピーを保持できるようにしたいのですが、生成されたクラスを使用して次のようなアイテムをロードすると、次のようになります。

$collection = Doctrine::getTable('Collection')->find(1);
$items = $collection->Items;

$itemsには私の複製が含まれていません。生成されたSQLは重複した行を正しく返すようです:

SELECT  i.id AS  i__id, i.name AS  i__name, c.id AS  c__id, c.collection_id AS  c__collection_id, c.item_id AS  c__item_id, FROM item i LEFT JOIN collection_item c ON i.id = c.item_id WHERE c.collection_id IN (?) - (1)

代わりに特定のdqlクエリを作成することでこれを回避できることは知っていますが、Itemsコレクションに重複を許可する簡単な設定があるかどうか誰かが知っていますか?

4

3 に答える 3

4

水分補給モードをHYDRATE_SCALARに変更する必要があります。

このハイドレーションモードは、重複データを含む可能性のあるフラット/長方形の結果セットを作成します。

$res = $q->execute(array(), Doctrine::HYDRATE_SCALAR);

(またはHYDRATE_NONEに)

http://www.doctrine-project.org/documentation/manual/1_1/en/working-with-models#fetching-dataに記載されているとおり

于 2009-11-01T15:19:25.657 に答える
1

Doctrine では、オブジェクトを複製することはできません。データベースから取得された各オブジェクトはDoctrineに一度だけ保存されます。同じオブジェクトを 2 回クエリすると、既に取得した同じオブジェクトへのポインターが取得されます。

オブジェクトを複製して にDoctrine_Collection保存できますが、コレクションを保存すると、実際にはデータベースに別の行が作成されます。

于 2009-04-30T02:43:33.223 に答える
0

試しましたか:

foreach($collection->Items as $item)
{
    // do something with $item
}

$collection->Items は実際の配列ではなく、ArrayAccess / ArrayIterator を実装するオブジェクトです。

于 2009-03-17T12:21:51.107 に答える