php - 查询生成器中属性的 DQL 访问属性(学说)

标签 php mysql doctrine-orm dql

我正在整理以下查询。

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

查询运行时没有崩溃但不起作用,因为我知道我有属于发票商店的数据库条目。它当前返回 0 个条目。我验证了 $store['id'] 给出了正确的 ID,所以这不是一个愚蠢的错误。删除该行及其参数引用会返回行,否则会很好。

问题是这一行,->leftJoin('inv.storeId','si')

是否允许我对来自另一个左连接的属性进行左连接?我需要这样做的原因是行项目没有引用商店 ID,只有发票有。 p>

我在文档中找不到关于这个主题的任何内容。

我也尝试过,但没有成功。

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

基本上我想做的是:

InvoiceLineItems 引用了一个发票。发票引用了商店

我正在尝试访问 invoice 对象(目前有效),然后访问 invoice 对象持有的 store 对象

最佳答案

我找到了问题的答案

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

事实证明代码 inv.storeId 确实有效。在我的例子中,这比尝试 leftJoin 更好。虽然我的问题在技术上没有得到解答,但这是我的问题的解决方案。 我仍然很好奇是否可以在您已经离开加入的属性上离开加入?

关于php - 查询生成器中属性的 DQL 访问属性(学说),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46993600/

相关文章:

php - 为什么 mysql 数据只打印电子邮件正文中的最后一行?

php - 动态表单字段插入

php - Codeigniter 中的数据库加载问题

mysql - 这段消除排列的SQL代码如何改进?

php - Symfony2 和 Doctrine2 - 设置 onDelete

mysql - "No result was found for query although at least one row was expected."查询应该在 Symfony 中显示记录

php - `str_replace` 有什么特别之处?

javascript - 如何为每个用户生成唯一 key ?

php - 根据表中选中的动态复选框更新 MySQL 数据库中的数据

symfony - 是否可以在OneupUploaderBundle中使用doctrine2持久保存不同的文件实体?