1

假设我有一个名为的实体Game,它有一个home_school_id. 如果我想要那所学校的 id当然可以$myGame->getHomeSchool()->getId(),但这会占用太多内存。我怎样才能直接得到home_school_id

4

1 回答 1

1

In your GameRepository.php. Then, do a $game->getHomeSchoolId($id); You'll just have to work with your select, from and where, but that's really easy.

    public function getHomeSchoolId($id)
    {
        return $this
                ->_em
                ->createQueryBuilder()
                ->select('q.home_school_id')
                ->from('BundleMyBundle:HomeSchool', 'q')
                ->where('q.something = :id')
                ->setParameter('id', $id)
                ->getQuery()
                ->getResult();
    }

If you want the ID of another entity, which is linked through a relation, you'll have to do a join. Just ask if you need more informations.

于 2012-09-05T13:50:50.877 回答