在 CakePHP 3 中按条件查找关联模型
Find by conditions on associated model in CakePHP 3
我有两个 table orders
和 sub_orders
。
他们的协会是
$orders->hasMany('SubOrders', [
'foreignKey' => 'order_id'
]);
两个 table 在 orders
和 sub_orders
中分别有 invoice_no
和 sub_invoice
列。
我必须从 orders
table 中找到包含关联 sub_orders
的记录,其中 $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]);
}
]);
我有两个 table orders
和 sub_orders
。
他们的协会是
$orders->hasMany('SubOrders', [
'foreignKey' => 'order_id'
]);
两个 table 在 orders
和 sub_orders
中分别有 invoice_no
和 sub_invoice
列。
我必须从 orders
table 中找到包含关联 sub_orders
的记录,其中 $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]);
}
]);