gpt4 book ai didi

cakephp - 在 CakePHP 3 中按关联模型的条件查找

转载 作者:行者123 更新时间:2023-12-02 12:13:31 27 4
gpt4 key购买 nike

我有两个表orderssub_orders。他们的关联是

$orders->hasMany('SubOrders', [
'foreignKey' => 'order_id'
]);

两个表的 orderssub_orders 中分别有 invoice_nosub_invoice 列。

我必须从包含关联 sub_ordersorders 表中查找记录,其中 $trackingId 将与 Orders.invoice_no 匹配> 或 SubOrders.sub_invoice

$findOrder = $this->Orders->find('all', [
'conditions' => [
'OR' => [
'Orders.invoice_no' => $trackingId,
'SubOrders.sub_invoice' => $trackingId
]
],
'contain' => [
'SubOrders'
]
]);

但这会产生错误

Column not found: 1054 Unknown column 'SubOrders.sub_invoice' in 'where clause'

最佳答案

尝试像这样进行查询:

$findOrder = $this->Orders->find()
->where(['Orders.invoice_no' => $trackingId])
->contain(['SubOrders' => function ($q) use ($trackingId) {
return $q

->where(['SubOrders.sub_invoice' => $trackingId]);
}
]);

关于cakephp - 在 CakePHP 3 中按关联模型的条件查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39558115/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com