我有兩個客戶數據表。我需要在兩列(primaryKey和customer_id)上進行連接。這裏描述的問題和解決方案如下: How can I join two tables on multiple columns in CakePHP 3?CakePHP 3 belongsTo通過多列使用默認值
也有一些包含customer和「global」數據的表。全球數據適用於所有客戶,並可通過customer_id「0」識別。
結果應該是這樣的:
SELECT * FROM
table1
INNER JOIN
table2
ON (table1.table2_id = table2.id AND
(table1.customer_id=table2.customer_id OR table2.customer_id=0))
WHERE 1;
是否有可能(如果有的話,如何)與CakePHP的關係,做到這一點?
更新:它似乎並沒有在
... INNER JOIN calibrations Calibrations ON ( Foods.calibration_id = Calibrations.id AND ( Foods.tenant_id = 'Calibrations.tenant_id' OR Calibrations.tenant_id = 0 ) AND Calibrations.id = (Foods.calibration_id) ) ...
月2日更新工作尚未
$this->belongsTo('Calibrations', [ 'foreignKey' => 'calibration_id', 'joinType' => 'INNER', 'conditions' => [ 'Foods.calibration_id = Calibrations.id', 'OR' => [ 'Foods.tenant_id' => 'Calibrations.tenant_id', 'Calibrations.tenant_id' => '0' ] ] ]);
結果:
對不起我的草率調查,我已經找到了解決辦法:
$this->belongsTo('Calibrations', [
'foreignKey' => 'calibration_id',
'joinType' => 'INNER',
'conditions' => [
'OR' => [
'Foods.tenant_id = Calibrations.tenant_id',
'Calibrations.tenant_id' => '0'
]
]
]);
結果
INNER JOIN calibrations Calibrations ON (
(
Foods.tenant_id = Calibrations.tenant_id
OR Calibrations.tenant_id = 0
)
AND Calibrations.id = (Foods.calibration_id)
)
那解...
請用英文。 – aynber
感謝翻譯很好 – chriss