2012-08-25 80 views
1

我有數組這樣如何從cake查詢數組中獲取正常的SQL查詢?

$conditions = array("Post.title" => "This is a post"); 

而且使用$條件,這種方法陣列。

$this->Post->find('first', array('conditions' => $conditions)); 

我想將$ conditions數組轉換爲正常的sql查詢。 我想用

$this->Post->query($converted_query); 

,而不是

$this->Post->find('first', array('conditions' => $conditions)); 
+0

只是因爲?或者你有沒有你沒有描述的目標? – tadman

+0

我有10個cakephp查詢。在某些情況下,我想結合這個查詢並在mysqli中執行組合查詢 – EbinPaulose

回答

0

,做你想做,你可以做兩件事情是什麼:

1)結合您的$conditions陣列,讓CakePHP的建設新的查詢,所以你可以簡單地再次使用$this->Model->find()

2)使用this。這是對mysql數據源的擴展,它增加了選項$this->Model->find('sql', array('conditions' => $conditions)),它將返回SQL查詢。此選項可能可能會造成麻煩,因爲對於某些查找調用(特別是在您提取相關模型時),CakePHP使用多個查詢來獲取關聯模型(特別是在hasMany-關聯的情況下)。

如果可能的話,選項1可能會造成最小的麻煩。使用2的另一個問題是,如果您試圖將兩個具有衝突條件的查詢結合起來(如查詢1中的'name = Hansel'和查詢2中的'name = Gretel'),除非您計劃編寫任何內容額外的代碼來解析結果查詢並尋找衝突。 與1一起使用可能會簡單得多,並且可能會避免很多問題。

1
$null=null; 
echo $this->getDataSource()->generateAssociationQuery($this, NULL, NULL, NULL, NULL, $query_array, false,$null);