模型.php
// Declare $datetime_limit
public datetime_limit;
控制器.php
// datetime_limit should be the actual datetime + 5 days
$criteria->select="DATE_ADD(NOW(), INTERVAL 5 DAY) AS datetime_limit";
错误信息:
Active record "Users" is trying to select an invalid column "DATE_ADD(NOW()". Note, the column must exist in the table or be an expression with alias.
Edit 1:
我想使用关系表(多对多)过滤带有条件的查找。
所以datetime_limit
不能有关系events.datetime
。
我怎样才能做到这一点?
$criteria->select=array("DATE_ADD(NOW(), INTERVAL 5 DAY) AS datetime_limit");
$criteria->with=array('events');
$criteria->having='datetime_limit!=`events`.`datetime`';
$models=Users::model()->findAll($criteria);
这个异常被抛出CActiveFinder::getColumnSelect https://github.com/yiisoft/yii/blob/1.1.12/framework/db/ar/CActiveFinder.php#L885.
When CDbCriteria::$select
is a string, it's treated as a simple list of comma-delimited columns. Your expression gets interpreted as two distinct columns. You can work around this by setting select
to an array yourself - in this case the comma splitting is not done1:
$criteria = new CDbCriteria();
$criteria->select = array("DATE_ADD(NOW(), INTERVAL 5 DAY) AS datetime_limit");
$models = Users::model()->findAll($criteria);
请注意,如果您编写的别名与公共模型属性或数据库字段不对应,它将被检索但会默默地被忽略 - 由于某种原因,Yii 不会为此抛出异常。
1 However, this function will still attempt to find a .
in the expression and interpret the part after it as a column identifier - don't use .
in your expression and you should be fine.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)