2009-02-14 48 views
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查詢代替,但沒有人知道是否有簡單的設置的地方,讓項目集合有重複?

回答

0

做了嘗試:

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

,如果我沒有記錯$收藏 - >產品是不是真正的陣列是實現了ArrayAccess/ArrayIterator

0

教義,你不能有重複的對象對象。從數據庫中檢索的每個對象只在Doctrine中存儲一次。如果你兩次查詢同一個對象,你會得到一個指針,它指向你已經檢索過的同一個對象。

您可以克隆該對象並將其存儲在您的Doctrine_Collection中,但當您保存該集合時,這實際上會在數據庫中創建另一行。