Yii1-CActiveDataProvider条件无法按预期工作

时间:2018-06-27 13:34:51

标签: yii yii1.x

我正在尝试将此查询转换为CActiveDataProvider

// SELECT p.id "
//     . "FROM projects AS p "
//     . "LEFT JOIN purged_files AS pf ON p.id = pf.project_id "
//     . "WHERE pf.id IS NULL "
//     . "AND `new_status_id` IN ('DELIVERED', 'PAID') "
//     . "AND `created` <= '" . date("Y-m-d H:i:s", strtotime("-" . $lifetime_days . " DAYS")) . "' "
//     . "ORDER BY id DESC "
//     . "LIMIT 0, 5000")

当前代码:

$criteria->select    = "project.id";
$criteria->join      = 'LEFT JOIN purged_files ON project.id = purged_files.project_id';
$criteria->order     = "project.id desc";
$criteria->condition = "`new_status_id` IN ('DELIVERED', 'PAID') AND `created` <= '" . date("Y-m-d H:i:s", strtotime("-180 DAYS")) . "'";
$criteria->limit     = 5000;
$criteria->offset    = 0;

$dataProvider = new CActiveDataProvider('Project', array(
    'criteria'   => $criteria,
));

$iterator = new CDataProviderIterator($dataProvider);

foreach ($iterator as $project) {
    echo $project->id . "<br>";
}

var_dump('<pre>', "end", '</pre>');die;

运行此命令时出现此错误:

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'project.id' in 'on clause'. The SQL statement executed was: SELECT COUNT(*) FROM `projects` `t` LEFT JOIN purged_files ON project.id = purged_files.project_id WHERE (`t`.`status` <> "DELETED" AND `t`.`status` <> "CANCELED" AND `t`.`status` <> "ARCHIVED") AND (`new_status_id` IN ('DELIVERED', 'PAID') AND `created` <= '2017-12-29 13:31:42')

这里似乎是什么问题?

1 个答案:

答案 0 :(得分:0)

您使用的别名不正确。主模型表的别名为t,因此您应根据情况使用t作为别名而不是project

$criteria->order = "t.id desc";

覆盖条件中的别名:

$criteria->alias = 'project';