问题描述
我为我的 Grid 表创建了一个资源模型,其中包含一个连接 2 个表的自定义查询。 sales_order
和 sales_payment_transaction
连接起来显示所有有付款的记录。以下是我的查询
protected function _initSelect()
{
parent::_initSelect();
$this->getSelect()
->joinLeft(
['spt' => $this->getTable('sales_payment_transaction')],'main_table.entity_id = spt.order_id',['spt.created_at as date_paid']
)
->where('main_table.status in ("complete","processing")')
->order('main_table.entity_id DESC');
$this->addFilterToMap('created_at','main_table.created_at');
return $this;
}
正如您在我的自定义列中看到的,我添加了一个名为 spt.created_at
的新列,名称为 date_paid
此新列用于日期过滤。因此,每当我按日期过滤订单时,都会使用 date_paid
作为参数。现在查看日志时出现此错误
SELECT COUNT(*) FROM `sales_order` AS `main_table`
LEFT JOIN `sales_payment_transaction` AS `spt`
ON main_table.entity_id = spt.order_id
WHERE (main_table.status in (\"complete\",\"processing\"))
AND (`date_paid` >= '2021-01-03 00:00:00')
AND (`date_paid` <= '2021-10-03 22:59:59')
/// Column not found: 1054 UnkNown column 'date_paid' in 'where clause'
它似乎无法识别新列。我可以知道如何正确构造此查询吗?
解决方法
您的报价好像在这里不显示:
['spt.created_at as date_paid']
应该是:
['spt.created_at' 作为 'date_paid']