doupu1727 2015-07-09 18:08
浏览 45
已采纳

Symfony Doctrine通过另一个实体获取实体

I have 3 entities: User, ImaginaryBankAccount and Recharge. One user has one ImaginaryBankAccount and ImaginaryBankAccount can have more than one Recharges. And I want to select from DB all Recharges that belongs to one user.

I have this query:

$resResults = $query->getResult();
$query = $em->createQuery('SELECT rec
                           FROM AppBundle:Recharge rec
                           WHERE rec.dateTime > :tresholdDate
                            AND rec.imaginaryBankAccount.user = :user
                           ORDER BY rec.dateTime'
)->setParameter('tresholdDate', $dateXDaysBack)
 ->setParameter('user', $filter->getUser());
$recResults = $query->getResult();

But it throws error:

[Semantical Error] line 0, col 223 near 'user = :user ': Error: Class AppBundle\Entity\Recharge has no field or association named imaginaryBankAccount.user

How can I achieve my goal with Doctrine2?

  • 写回答

1条回答 默认 最新

  • douxue4242 2015-07-09 18:10
    关注

    You have to add a JOIN clause with your imaginaryBankAccount relation like this :

    SELECT rec
    FROM AppBundle:Recharge rec
    JOIN rec.imaginaryBankAccount i
    WHERE rec.dateTime > :tresholdDate
    AND i.user = :user
    ORDER BY rec.dateTime
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?