【发布时间】:2019-05-21 13:04:56
【问题描述】:
SQL - 是正确的并且在 mysql-console 中工作。我有一些 SQL 查询,用于从数据库产品中进行选择。我试图将它放入存储库,但它根本不起作用。
这里还有 ProductRepository 的功能代码
有人可以帮助我或显示我的错误并解释如何将 SQL 转换为查询构建器查询或强制使用纯 SQL 吗?
SELECT *
FROM accounting_products
LEFT JOIN accounting_incoming_invoice_items
ON accounting_incoming_invoice_items.product_id = accounting_products.id
WHERE accounting_incoming_invoice_items.outcome_price >
accounting_incoming_invoice_items.price;
public function findAllOnStorage()
{
$rsm = new ResultSetMapping($this->getEntityManager());
$query = $this->getEntityManager()->createNativeQuery(
"SELECT * FROM accounting_products LEFT JOIN accounting_incoming_invoice_items
ON accounting_incoming_invoice_items.product_id = accounting_products.id
WHERE accounting_incoming_invoice_items.outcome_price > accounting_incoming_invoice_items.price;",
$rsm);
$all = $query->getResult();
#dump($all);exit;
return $all;
}
如果在存储库中纯 SQL - 它只是不起作用。查询的结果是空集,但是在控制台中,它只返回一条记录
尝试将 SQL 转换为查询构建器没有任何成功。
【问题讨论】:
-
您的 LEFT JOIN 返回常规的 INNER JOIN 结果。将 accounting_incoming_invoice_items 条件从 WHERE 移到 ON 以获得真正的 LEFT JOIN 结果。
标签: php sql symfony doctrine left-join