我有3张桌子:
他们之间的关系:
银行模式:
public function relations() {
return array(
'bankBankDetails' => array(self::HAS_MANY, 'BankBankDetails', 'bank_id'),
);
}
bank_details模型:
public function relations() {
return array(
'bankBankDetails' => array(self::HAS_MANY, 'BankBankDetails', 'bank_details_id'),
);
}
bank_bank_details型号:
public function relations()
{
return array(
'bank' => array(self::BELONGS_TO, 'Bank', 'bank_id'),
'bankDetails' => array(self::BELONGS_TO, 'BankDetails', 'bank_details_id'),
);
}
我想在 bank_details 模型的管理视图中获取银行详细信息,例如bank_name,ifsc etc info。
我生成的普通SQL查询非常正常:
SELECT b.name
FROM bank b
LEFT JOIN bank_bank_details bbd ON bbd.bank_id = b.bank_id
LEFT JOIN bank_details bd ON bd.bank_details_id = bbd.bank_details_id
WHERE bd.bank_details_id = bbd.bank_details_id
LIMIT 0 , 30
现在我只想将它与Yii CDBCriteria整合,但它不适合我。请检查以下代码:
public function search() {
$criteria = new CDbCriteria;
// select b.name
// from bank b
// left join bank_bank_details bbd
// on bbd.bank_id = b.bank_id
// left join bank_details bd on bd.bank_details_id = bbd.bank_details_id
// WHERE bd.bank_details_id = bbd . bank_details_id;
$criteria->compare('bank_details_id', $this->bank_details_id);
$criteria->compare('first_holder_name', $this->first_holder_name, true);
$criteria->compare('nominee1', $this->nominee1, true);
$criteria->select = 'b.name';
$criteria->join = 'LEFT JOIN bank_bank_details bbd ON bbd.bank_id = b.bank_id ';
$criteria->join .= 'LEFT JOIN bank_details bd ON bd.bank_details_id = bbd.bank_details_id';
$criteria->condition = 'bd.bank_details_id = bbd.bank_details_id';
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'pagination' => array(
'pageSize' => 10,
),
));
}
错误: 发现错误500 CDbCommand无法执行SQL语句:SQLSTATE [42S22]:找不到列:1054'on clause'中的未知列'b.bank_id'
任何帮助都将不胜感激。
答案 0 :(得分:1)
您需要像这样为表设置别名:
$criteria->alias='b';
或使用默认别名“t”而不是“b”
阅读更多内容 http://www.yiiframework.com/doc/api/1.1/CDbCriteria#alias-detail
答案 1 :(得分:1)
尝试:
public function search() {
$criteria = new CDbCriteria;
$criteria->compare('bank_details_id', $this->bank_details_id);
$criteria->compare('first_holder_name', $this->first_holder_name, true);
$criteria->compare('nominee1', $this->nominee1, true);
$criteria->select = 'bank.name';
$criteria->with = array(
'bankBankDetails' => array('joinType'=>'LEFT JOIN'),
'bankBankDetails.bank' => array('joinType'=>'LEFT JOIN'),
);
$criteria->addCondition('t.bank_details_id = bankBankDetails.bank_details_id');
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'pagination' => array(
'pageSize' => 10,
),
));
}
主表(在FROM中)具有别名t
,在compare
之后你已经有了一些条件,而不是你需要添加到现有的,而不是重写
<强>更新强>
如果您从模型bank_details
进行查询,则主表(FROM
)必须为bank_details
。查询将是:
SELECT b.name
FROM bank_details bd
LEFT JOIN bank_bank_details bbd ON bd.bank_details_id = bbd.bank_details_id
LEFT JOIN bank b bbd ON bbd.bank_id = b.bank_id
WHERE bd.bank_details_id = bbd.bank_details_id
LIMIT 0 , 30
答案 2 :(得分:0)
这里
$criteria->select = 'b.name';
您只选择银行名称而未选择ID
不确定yii格式是否类似
$criteria->select = 'b.name,b.bank_id';
或
$criteria->select = array('b.name,b.bank_id');
答案 3 :(得分:0)
正如@Evgeniy所提到的,您需要使用$criteria->alias = 'b';
http://www.yiiframework.com/doc/api/1.1/CDbCriteria#alias-detail
您可能还想查看使用CDbCriteria::with
您可以使用它来选择相关模型。
http://www.yiiframework.com/doc/api/1.1/CDbCriteria#with-detail
如果您使用上述内容,请确保您也正确使用CDbCriteria::together
。要一次选择所有相关模型,请将其设置为true
;
http://www.yiiframework.com/doc/api/1.1/CDbCriteria#together-detail