[英]CakePHP 1.3 query question
我試圖將工作查詢轉換為CakePHP查找。 所以我失敗了,嘗試使用可容納行為等等,但是沒有運氣。 請幫助我將此SQL查詢轉換為CakePHP查找:
SELECT COUNT(DISTINCT(da.dealer_id))
FROM dealer_access_list da, dealers d
WHERE da.dealer_id NOT IN (SELECT dealer_id FROM fixture_inventory)
AND d.disabled = 0
AND d.do_not_include IS NOT TRUE
AND da.dealer_id = d.dealer_id
謝謝!
我在連接兩個以上的表以及使用SQL特殊功能時遇到問題。 我想使用CakePHP ORM,而不要使用普通SQL。
蛋糕曾經有-! 在查找中運行SQL的語法,這對於嵌套查詢會很方便。 但是據我所知,不建議使用它,您可能必須將其分成兩部分。
我將使用兩個查詢將答案放在這里,直到有人可以使用嵌套查詢提出替代方案為止
$dealers = //get the dealer IDs in the table in the normal way. make sure it's an array of only the IDs
$opts = array(
'fields' => array(
'COUNT(DISTINCT(da.dealer_id)) AS count_dealer_id'
),
'conditions' => array(
'NOT' => array('da.dealer_id' => $dealers),
'd.disabled' => 0,
'd.do_not_include IS NOT TRUE',
)
)
$results = $this->find('all', $opts); //assuming you're in the correct model
只要您在模型中正確設置了關聯,cake就可以找出正確的聯接,而您不必擔心。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.