关联或遏制错误

时间:2011-03-23 19:00:39

标签: cakephp cakephp-1.3

这是我知道自己做错了什么的时候之一,但在看到它时我显然是聋哑,愚蠢和盲目。我希望另一双眼睛可以打开我自己的眼睛。

我有ZipCode模型和Incentive模型。有一个荣耀的连接表(因为它有自己的密钥而得到荣耀)坐在中间。联接表包含idincentive_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模型通过$useTable绑定到 MySql视图,而不是表格。我没有看到任何暗示在这种情况下它应该是一个问题,但它是非标准的。

如果你看到我遗失的内容,请拨打911或至少回答一下。

1 个答案:

答案 0 :(得分:1)

罗布

移动条件

  'ZipCode.zip' => $zip 

要包含这样的声明

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

然后像往常一样继续你的陈述的其余部分