du5591 2017-10-28 19:25
浏览 43
已采纳

查询生成器中属性的DQL访问属性(Doctrine)

I have the following query I am putting together.

$lineItems      = $em->createQueryBuilder()->select('invitems.unitprice,invitems.quantity,invitems.tax,invitems.scrapetax')
                                      ->from('AppBundle:InvoiceLineItems', 'invitems')
                                      ->leftJoin('invitems.invoiceId','inv') //StoreID is present in the invoice so only need to leftjoin here
                                      ->leftJoin('inv.storeId','si')
                                      ->where('inv.companyId = :companyId')
                                      ->andWhere('si.id = :storeId')
                                      ->andWhere('inv.created BETWEEN :startdate AND :enddate')
                                      ->andWhere("inv.status = 'sent' or inv.status = 'paid'  or inv.status = 'partial' ")
                                      ->setParameter('startdate', $startDate)
                                      ->setParameter('enddate', $endDate)
                                      ->setParameter('companyId',$companyid)
                                      ->setParameter('storeId',$store['id'])
                                      ->getQuery()
                                      ->getResult();

The query runs without crashing but does not work since I know I have database entries that belong to a store for the invoice. It currently returns 0 entries. I verified $store['id'] is giving correct ID so its not a silly error. Removing that line and its parameter reference returns rows just fine otherwise.

The problem is this line, ->leftJoin('inv.storeId','si')

Am I allowed to left join on a property that came from another left join? The reason I need to do this is the lineitems do not have reference to the storeID and only invoice does.

I can't find in the documentation anything about this subject in particular.

I tried this also without any success.

->andWhere('inv.storeId = :storeId')

Essentially what I'm trying to do is this:

InvoiceLineItems has reference to an Invoice. Invoice has reference to a Store

I'm trying to get access to the invoice object (which works currently) but then access to the store object that the invoice object holds.

  • 写回答

1条回答 默认 最新

  • duanmanpi9358 2017-10-29 21:37
    关注

    I found the answer to my question

    $lineItems      = $em->createQueryBuilder()->select('invitems.unitprice,invitems.quantity,invitems.tax,invitems.scrapetax')
                                          ->from('AppBundle:InvoiceLineItems', 'invitems')
                                          ->leftJoin('invitems.invoiceId','inv') //@JA - StoreID is present in the invoice, only need to leftjoin in this case
                                          ->where('inv.companyId = :companyId')
                                          ->andWhere('inv.storeId = :storeId')
                                          ->andWhere('inv.created BETWEEN :startdate AND :enddate')
                                          ->andWhere("inv.status = 'sent' or inv.status = 'paid'  or inv.status = 'partial' ")
                                          ->setParameter('startdate', $startDate)
                                          ->setParameter('enddate', $endDate)
                                          ->setParameter('companyId',$companyid)
                                          ->setParameter('storeId',$store["id"])
                                          ->getQuery()
                                          ->getResult();
    

    It turns out the code inv.storeId DOES work. This is better than trying to leftJoin in my case. While my question is not technically answered this was the solution to my problem. I am still curious if its possible to leftJoin on a property that you had already leftJoined on?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab