1

我有一个通过关联与实体Stats相关的实体。JourneyManyToOne

id:
    index:
        type: integer

fields:
        idJourney:
        type: string
// data fields...
manyToOne:
    journey:
        targetEntity: Journey
        joinColumn:
            name: idJourney
            referencedColumnName: idJourney

与实体通过两个关联关联:一个与 中的第一个关联Journey,一个与最后一个关联。StationManyToOneStationJourney

id:
    idjourney:
        type: string
    fields:
        idFirstStation:
            type: string
        idLastStation:
            type: string
    // other info fields
    manyToOne:
        firstStation:
            targetEntity: Station
            joinColumn:
                name: idFirstStation
                referencedColumnName: idStation
        lastStation:
            targetEntity: Station
            joinColumn:
                name: idLastStation
                referencedColumnName: idStation

最后,Station实体:

id:
    idStation:
        type: string
fields:   
    name:
        type: string
// other station info

我通过一个工作正常的自定义存储库方法检索Stats具有所有相关子对象的对象集合。

$statCollection = $statsRepository->getStatsByDateAndArea($date, $area);

//This retrieves the expected data
$statCollection[0]->getJourney()->getFirstStation()->getName();

但是,使用循环遍历集合foreach不起作用:

foreach($statCollection as $stat) {
    $journey = $stat->getJourney(); // works fine
    $firstStationId = $journey->getFirstStationId(); // works too
    $firstStation = $journey->getFirstStation(); // still works, but returns a Proxies\AcmeAppPathWhateverBundleEntityStationProxy object instead of a AcmeAppPathWhateverBundleEntityStation, but this should be transparent (as per Doctrine documentation)
    $firstStationName = $firstStation->getName(); // throws an EntityNotFoundException
}

知道发生了什么吗?我应该强制 Doctrine 获取所有子实体吗?

编辑 错误消息相当简洁:

EntityNotFoundException: Entity was not found.

不是很有帮助...

4

1 回答 1

2

我最终在我的自定义存储库方法中明确查询了完整的子实体集......

我改变了这个查询:

    ->select('stats')
    ->leftJoin('stats.journey', 'j')
    ->leftJoin('j.firstStation', 'fs')
    ->leftJoin('j.lastStation', 'ls')

至 :

    ->select('stats, j, fs, ls')
    ->leftJoin('stats.journey', 'j')
    ->leftJoin('j.firstStation', 'fs')
    ->leftJoin('j.lastStation', 'ls')

我猜 Doctrine 对 Proxy 对象的使用并不是那么透明......

于 2012-08-30T09:47:23.073 回答