Symfony2 - EntityNotFoundException

时间:2012-08-29 14:26:17

标签: symfony twig

我有一个Stats实体,通过Journey关联与ManyToOne实体相关。

id:
    index:
        type: integer

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

Journey通过两个Station关联与ManyToOne实体相关:一个用于Station的第一个Journey,另一个用于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

我通过自定义Repository方法检索包含所有相关子对象的$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 } 循环遍历集合并不起作用:

EntityNotFoundException: Entity was not found.

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

修改 错误消息相当简洁:

{{1}}

不是很有帮助......

1 个答案:

答案 0 :(得分: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对象的使用并不是那么透明......

相关问题