2009-02-14 57 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中,但当您保存该集合时,这实际上会在数据库中创建另一行。

相关问题