这是我知道自己做错了什么的时候之一,但在看到它时我显然是聋哑,愚蠢和盲目。我希望另一双眼睛可以打开我自己的眼睛。
我有ZipCode
模型和Incentive
模型。有一个荣耀的连接表(因为它有自己的密钥而得到荣耀)坐在中间。联接表包含id
,incentive_id
和zip
字段(旧数据库)。我的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或至少回答一下。
答案 0 :(得分:1)
罗布
移动条件
'ZipCode.zip' => $zip
要包含这样的声明
array(
'contain' => array( 'ZipCode'=>
array('conditions'=>
array('ZipCode.zip' => $zip ))),
然后像往常一样继续你的陈述的其余部分