gpt4 book ai didi

php - 选择带有条件和别名的表达式

转载 作者:行者123 更新时间:2023-11-29 23:27:50 25 4
gpt4 key购买 nike

在 MySql 中你可以有这样的查询

SELECT d., d.is_new, 
(
d.distance - 1
) AS `dist`
FROM `table` AS d
WHERE d.is_new!='1'
HAVING `dist` <= 10

如何在 Yii 1.1.15 中使用我的模型中的 scopes 函数 来完成这项工作?现在我有这个

public function scopes()
{
return array(
'_distance'=>array(
'alias' => 'd',
'select'=>array('d.id, d.is_new,
(
d.distance - 1
) AS `dist`'
),
'having'=>'`dist` <= 10',
'condition'=>'d.is_new!="1"'
)
);
}

查询有效,只是当我调用 _distance 时它不返回 dist 列。当我在 phpmyadmin 中运行查询时,它也工作正常。

$dataProvider=new CActiveDataProvider(Bla::model()->_distance(), ..... );

知道我错过了什么吗?

最佳答案

尝试在 Active Record 类中添加公共(public) $dist 属性。

执行 Active Record 查找查询时,会对每个结果行调用 populateRecord() 并检查模型属性是否存在或数据库架构中是否定义了列名。 http://www.yiiframework.com/doc/api/1.1/CActiveRecord#populateRecord-detail

class Bla extends CActiveRecord
{
public $dist;

//rest of your model
}

$model->dist 现在应该可以在使用您的范围的实例上工作。

关于php - 选择带有条件和别名的表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26817938/

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