2015-07-09 18:08
浏览 44

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?

图片转代码服务由CSDN问答提供 功能建议

我有3个实体:User,ImaginaryBankAccount和Recharge。 一个用户有一个ImaginaryBankAccount,而ImaginaryBankAccount可以有多个Recharges。 我想从DB中选择属于一个用户的所有Recharges。


  $ 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(); 


[语义错误] 第0行,第223页'user =:user '附近:错误:类AppBundle \ Entity \ Recharge没有名为imaginaryBankAccount.user的字段或关联

如何 我可以用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
    解决 无用
    打赏 举报

相关推荐 更多相似问题