Symfony2 - EntityNotFoundException [英] Symfony2 - EntityNotFoundException

查看:46
本文介绍了Symfony2 - EntityNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Stats entity that is related to a Journey entity through a ManyToOne association.

id:
    index:
        type: integer

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

JourneyStation 实体通过两个 ManyToOne 关联:一个用于第一个 Station旅程,最后一程.

The Journey is related to the Station entity through two ManyToOne association: one for the first Station of the Journey, one for the last.

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 对象和所有相关的子对象.

I retrieve a collection of Stats objects with all related sub-objects via a custom Repository method which works fine.

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

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

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

However, iterating through the collection with a foreach loop doesn't work:

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 获取所有子实体吗?

Any idea what's going on ? Should I force Doctrine to fetch all sub entities ?

编辑错误消息相当简洁:

EntityNotFoundException: Entity was not found.

不是很有帮助...

推荐答案

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

I ended up querying explicitly for the full set of sub-entities in my custom repository method...

我更改了此查询:

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

I guess Doctrine's use of Proxy objects are not all that transparent...

这篇关于Symfony2 - EntityNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆