gpt4 book ai didi

php - Yii CDBCriteria 复杂连接查询

转载 作者:行者123 更新时间:2023-11-29 08:19:06 27 4
gpt4 key购买 nike

我有 3 张 table :

  1. 银行
  2. 银行详细信息
  3. bank_bank_details

它们之间的关系:

银行模式:

public function relations() {
return array(
'bankBankDetails' => array(self::HAS_MANY, 'BankBankDetails', 'bank_id'),
);
}

银行详细信息模型:

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模型的管理 View 中获取银行详细信息,例如bank_name、ifsc等信息。

我生成的正常 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 子句' 中的未知列 'b.bank_id'

如有任何帮助,我们将不胜感激。

最佳答案

您需要为表设置别名,如下所示:$criteria->alias='b'; 或使用默认别名“t”而不是“b”

阅读更多内容 http://www.yiiframework.com/doc/api/1.1/CDbCriteria#alias-detail

关于php - Yii CDBCriteria 复杂连接查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19923306/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com