どのように私は教義コレクション内の重複レコードを許可することができます

StackOverflow https://stackoverflow.com/questions/548346

  •  23-08-2019
  •  | 
  •  

質問

私はいくつかの仲間の教義のユーザーがそこにあります願っています。
ここに私の関係の簡略化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;

$アイテム私の重複が含まれていません。生成された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)

私はどこかItemsコレクションが重複を持つことができるようにする簡単な設定がある場合、私は私ではなく、特定のDQLクエリを作成するが、誰もが知っているこの問題を回避することができます知っていますか?

役に立ちましたか?

解決

あなたは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する

他のヒント

あなたがしようとしました。

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

私が正しく$集・>アイテムを思い出すならば、それはArrayAccessの/ ArrayIteratorを実装するオブジェクトの実際の配列ではありません。

教義では、重複したオブジェクトを持つことはできません。データベースから検索された各オブジェクトは、教義に一度だけ記憶されます。あなたが同じオブジェクトに対してクエリを実行した場合二回あなたがすでに取得しています。

ポインタと同じオブジェクトを取得します

あなたは、オブジェクトのクローンを作成し、あなたのDoctrine_Collectionに保管していますが、コレクションを保存するときには、実際にはデータベース内の別の行を作成しますすることができます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top