2011-03-23 52 views
0

這是我知道自己做錯事情的那些年,但我看起來很明顯是聾啞,盲目。我希望另一雙眼睛能夠打開我自己的眼睛。關聯或遏制錯誤

我有一個ZipCode模型和一個Incentive模型。有一個榮耀的連接表(榮耀,因爲它有自己的鑰匙)坐在中間。連接表具有id,incentive_idzip字段(遺留數據庫)。如我ZipCode模型HABTM Incentive

public $hasAndBelongsToMany = array(
    'Incentive' => array(
    'with'     => 'ZipCodeIncentive', 
    'foreignKey'   => 'zip', 
    'associationForeignKey' => 'incentive_id', 
), 
); 

Incentive模型HABTM ZipCode如下:

public $hasAndBelongsToMany = array(
    'ZipCode' => array(
    'with'     => 'ZipCodeIncentive', 
    'foreignKey'   => 'incentive_id', 
    'associationForeignKey' => 'zip', 
), 

我有一個方法,ZipCode::incentives($zip)是希望所有的獎勵相關拉到指定的郵政編碼:

$incentives = $this->Incentive->find(
    'all', 
    array(
    'contain' => array('ZipCode'), 
    'fields'  => array('Incentive.id', 'Incentive.name', 'Incentive.it_name', 'Incentive.amount', 'Incentive.state', 'Incentive.entire_state', 'Incentive.excluded', 'Incentive.is_active'), 
    'conditions' => array(
     'Incentive.excluded' => 0, 
     'Incentive.is_active' => 1, 
     'OR' => array(
     'Incentive.state' => 'US', # nationwide incentives 
     array(
      # Incentives that apply to the entire state the zip code belongs to 
      'Incentive.entire_state' => 1, 
      'Incentive.state'  => $state, 
     ), 
     'ZipCode.zip' => $zip # specific to the building's zip code 
    ) 
    ), 
    'order' => array('Incentive.amount DESC'), 
) 
); 

我得到的麻煩是以下錯誤:

SQL Error: 1054: Unknown column 'ZipCode.zip' in 'where clause' 

ZipCode模型的表是沒有得到參加了SQL,但我還沒有掌握爲什麼尚未。值得一提的是,Incentive模型綁定到MySql視圖,而不是表格,通過$useTable。在這種情況下,我沒有看到任何暗示它應該成爲問題的東西,但它是非標準的。

如果您發現我錯過了什麼,請致電911或至少放棄一個答案。

回答

1

羅布

移動狀態

'ZipCode.zip' => $zip 

要將包含這樣

array(
    'contain' => array('ZipCode'=> 
          array('conditions'=> 
            array('ZipCode.zip' => $zip))), 

聲明,然後用你的話的其餘部分繼續像往常一樣

+0

衛生署!我知道這將會是這樣的。不像我預計的那樣明顯,但我感謝你的幫助。這一直在一天中更好的時候吸菸。如果SO會讓我這樣做,我會標記你的答案兩次。 – 2011-03-24 01:10:03