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 中只存储一次。如果您两次查询同一个对象,您将获得一个与您已经检索到的相同对象的指针。

您可以克隆对象并将其存储在您的 中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 回答