属性 "CDbCriteria.:centerId" 未在 yii 中定义

Property "CDbCriteria.:centerId" is not defined in yii

我在 yii 中使用 select 方法它给出错误 "Property "CDbCriteria.:centerId" is not defined"

if (0 < self::model()->countByAttributes(
    'centerId = :centerId AND qTypeId = :qTypeId',
    array(
        ':centerId' => $centerId,
        ':qTypeId'  => $qTypeId,
    )
)) {
    throw new Exception('Duplicate Entry for center and que type');
}

您使用此方法的方式有误。您跳过了第一个参数,它应该是用作过滤器的活动记录参数列表 (see documentation)。你可能需要这样的东西:

if (0 < self::model()->countByAttributes([
    'centerId' => $centerId,
    'qTypeId'  => $qTypeId,
]) {
    throw new Exception('Duplicate Entry for center and que type');
}

或使用count():

if (0 < self::model()->count(
    'centerId = :centerId AND qTypeId = :qTypeId',
    [
        ':centerId' => $centerId,
        ':qTypeId'  => $qTypeId,
    ]
)) {
    throw new Exception('Duplicate Entry for center and que type');
}