condition in criteria in yii framework php(yii框架php中的条件条件)
问题描述
$criteria=new CDbCriteria();
$criteria->with = array('reviewCount', 'category10', 'category20', 'category30', 'town');
$criteria->select = 't.id,business,street,postalCode,contactNo,checkinCount,count(tbl_abc.id) as spcount';
$criteria->join = 'left join tbl_abc on t.id=tbl_abc.businessId';
$criteria->group = 't.id';
$criteria->order = 'spcount DESC';
$criteria->condition='spcount>1';
$bizModel = new CActiveDataProvider(Business::model(), array(
'criteria' => $criteria
));
我收到此错误:
Column not found: 1054 Unknown column 'spcount' in 'where clause'
如果我省略条件查询工作正常&按 spcount 订购企业.那么我如何重写这个查询,以便获得 spcount 大于 1 的所有业务?
If I omit the condition the query works fine & orders businesses by spcount. So how do I rewrite this query such that I get all the businesses whose spcount is greater than 1?
推荐答案
据我所知,你不能在 WHERE
部分(证明链接).删除条件行并添加以下内容:
As far as I know, you can't reference aliases in a WHERE
part (proof link). Remove the condition line and add the following:
$criteria->having = 'COUNT(tbl_abc.id) > 1';
更新
CActiveDataProvider
接受 finder 实例,所以你需要一个模型范围:
CActiveDataProvider
accepts finder instance, so you'll need a model scope:
<?php
class Business extends CActiveRecord
{
public function scopes()
{
return array(
'hasSpcount' => array(
'with' => array('reviewCount', 'category10', 'category20', 'category30', 'town'),
'select' => 't.id,business,street,postalCode,contactNo,checkinCount,count(tbl_abc.id) as spcount',
'join' => 'left join tbl_abc on t.id=tbl_abc.businessId',
'group' => 't.id',
'order' => 'spcount DESC',
'having' => 'COUNT(tbl_abc.id) > 1',
),
);
}
}
// usage
$provider = new CActiveDataProvider(Business::model()->hasSpcount());
希望这有效
这篇关于yii框架php中的条件条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:yii框架php中的条件条件
基础教程推荐
- HTTP 与 FTP 上传 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01