2011-03-23 61 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