2013-11-15 42 views
0

我最近升級我的1.3應用2.4 https://github.com/dereuromark/upgrade
但現在的$這個 - >型號 - >找到一些用法()沒有按沒有工作。特別是與相關模型/表格的連接。
產生「where列中的未知列'Bill.customer_id'」。升級1.3後 - > 2.4:找到()不參加相關車型

我的設置:

表和協會http://i.stack.imgur.com/bjOIz.png(圖)

模式

App::uses('AppModel', 'Model'); 
class Customer extends AppModel { 
public $actsAs = array('Logable', 'Containable'); 
public $hasMany = array(
    'Bill' => array(
     'className' => 'Bill', 
     'foreignKey' => 'customer_id', 
     'dependent' => false, 
     'conditions' => '', 
     'fields' => '', 
     'order' => '', 
     'limit' => '', 
     'offset' => '', 
     'exclusive' => '', 
     'finderQuery' => '', 
     'counterQuery' => '' 
    )... 
----------------------------------------------------------------- 
App::uses('AppModel', 'Model'); 
class Bill extends AppModel { 
public $actsAs = array('Containable', 'Logable', 'Lockable'); 

public $belongsTo = array(
    'Customer' => array(
     'className' => 'Customer', 
     'foreignKey' => 'customer_id', 
     'conditions' => '', 
     'fields' => '', 
     'order' => '' 
    )... 

public $hasAndBelongsToMany = array(
    'Stage' => array(
     'className' => 'Stage', 
     'joinTable' => 'bills_stages', 
     'foreignKey' => 'bill_id', 
     'associationForeignKey' => 'stage_id', 
     'unique' => true, 
     'conditions' => '', 
     'fields' => '', 
     'order' => '', 
     'limit' => '', 
     'offset' => '', 
     'finderQuery' => '', 
     'deleteQuery' => '', 
     'insertQuery' => '' 
    )... 
-------------------------------------------------------------- 
App::uses('AppModel', 'Model'); 
class BillsStage extends AppModel { 
public $actsAs = array('Containable', 'Logable', 'Lockable'); 

public $belongsTo = array(
    'Bill' => array(
     'className' => 'Bill', 
     'foreignKey' => 'bill_id', 
     'conditions' => '', 
     'fields' => '', 
     'order' => '' 
    ), 
    'Stage' => array(
     'className' => 'Stage', 
     'foreignKey' => 'stage_id', 
     'conditions' => '', 
     'fields' => '', 
     'order' => '' 
)... 
-------------------------------------------------------- 
App::uses('AppModel', 'Model'); 
class Stage extends AppModel { 
public $displayField = 'name'; 
public $actsAs = array('Tree', 'Containable', 'Logable'); 

public $hasMany = array(
    'Bill' => array(
     'className' => 'Bill', 
     'foreignKey' => 'stage_id', 
     'dependent' => false, 
     'conditions' => '', 
     'fields' => '', 
     'order' => '', 
     'limit' => '', 
     'offset' => '', 
     'exclusive' => '', 
     'finderQuery' => '', 
     'counterQuery' => '' 
    )... 

public $hasAndBelongsToMany = array(
    'Bill' => array(
     'className' => 'Bill', 
     'joinTable' => 'bills_stages', 
     'foreignKey' => 'stage_id', 
     'associationForeignKey' => 'bill_id', 
     'unique' => true, 
     'conditions' => '', 
     'fields' => '', 
     'order' => '', 
     'limit' => '', 
     'offset' => '', 
     'finderQuery' => '', 
     'deleteQuery' => '', 
     'insertQuery' => '' 
    )... 

在1.3的連接被工作。在2.4中只有一維sql查詢。

有什麼想法?

- 編輯 -

$billsStages = $this->Customer->Bill->BillsStage->find('all', array(
    'conditions' => array(
     'Bill.customer_id' => $this->Customer->id, 
     'Stage.id' => array_keys($vk_stages) 
    ), 
    'order' => 'BillsStage.created DESC' 
)); 
+0

你的'$ this-> Model-> find()'是怎麼樣的? –

+0

對不起,添加了我的find() - 現在打電話。請看一看。 @CTravel – KommiZZar

+0

模型的遞歸值是什麼?它們是可容納的嗎? – Nunser

回答

0

嘗試聯接

$options['joins'] = array(
    array('table' => 'bills', 
     'alias' => 'Bill', 
     'type' => 'LEFT', 
     'conditions' => array(
      'Bill.customer_id' => $this->Customer->id, 
     ) 
    ), 
    array('table' => 'stages', 
     'alias' => 'Stage', 
     'type' => 'LEFT', 
     'conditions' => array(
      'Stage.id' => array_keys($vk_stages) 
     ) 
    ) 
); 

$options['conditions'] = array(); 

$items= $this->Customer->Bill->BillsStage->find('all', $options); 

(從docs取出例子)(調整到你的情況)。

根據this answer,對於輔助模型上的條件,contain是不可能的,因爲contain進行了不同的查詢。因此,如果您希望在二級模型上應用條件,請使用連接,或者在您的模型中創建一個簡單函數,該函數執行兩個單獨的查詢,並創建一個宏數組,以便在使用contain時返回混合結果。

+0

'條件'與Stage.id仍然是一個'未知的列'。如果我刪除條件 - >左連接將完成,但結果1,600萬條目。整桌只有20k。我知道這個問題2天了。無法想象它爲什麼在1.3上工作,而不是在2.4上工作。研究了cakephp的所有更新日誌。 – KommiZZar

+0

那麼......這就是爲什麼我說你需要適應你的情況。在舞臺上是另一張桌子,那麼需要一個連接而不是一個條件。我更新了。我不知道表格的所有定義,所以我假設你有'bill_stages'和'bill_id'和'stage_id',所以爲什麼不嘗試在條件中放置'stage_id => array_keys($ vk_stages)'' '看看它是怎麼回事...... – Nunser

+0

當然,我試過了。但左邊的連接是爆裂的。結果太多了。我在你的桌子上看到:http://i.stack.imgur.com/bjOIz.png我的一般問題不僅僅是這種情況。我的老1.3應用程序中有更多這種類型。我想了解最新的變化。修復每一個發現電話會受到傷害。 ;-) 謝謝。 – KommiZZar