【问题标题】:Symfony2 - EntityNotFoundExceptionSymfony2 - EntityNotFoundException
【发布时间】:2012-08-29 14:26:16
【问题描述】:

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

id:
    index:
        type: integer

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

Journey 通过两个ManyToOne 关联与Station 实体相关联:一个用于Journey 的第一个Station,一个用于最后一个。

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

我通过一个工作正常的自定义 Repository 方法检索 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.

不是很有帮助...

【问题讨论】:

  • 您能否提供更多有关抛出异常(消息等)的详细信息?
  • 带有完整错误消息的已编辑问题。

标签: symfony twig


【解决方案1】:

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

我改变了这个查询:

    ->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 对象的使用并不是那么透明……

【讨论】:

    猜你喜欢
    • 2014-10-16
    • 2013-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-15
    相关资源
    最近更新 更多