模型搜索方法
$criteria->alias = 'c';
$criteria->select = 'c.*,max(ca.date) AS lastactivity';
$criteria->join = 'LEFT JOIN tbl_contact_action AS ca ON (ca.contact_id=c.contact_id)';
$criteria->condition = 'c.status<>"Deleted"';
$criteria->group = 'c.contact_id';
$criteria->order = 'lastactivity DESC';
$sort = new CSort;
$sort->defaultOrder = array('lastactivity' => CSort::SORT_DESC); //'name ASC';
$sort->attributes = array(
'name' => 'name',
'email' => 'email',
'status' => 'status',
'createdon' => 'createdon',
'lastactivity' => 'lastactivity',
);
$sort->applyOrder($criteria);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'sort' => $sort,
));
基本上,我有一个1:n的关系,我只需要来自子表的最新记录。将根据子表中最新完成的注释显示父表数据。如何使这个字段可以排序?
错误
CDbCommand无法执行SQL语句:SQLSTATE [42S22]:找不到列:1054'order clause'中的未知列'c.lastactivity'。
答案 0 :(得分:1)
只是一个理论: 因为你有一个连接在一起的sql,所有数据将在1个结果中。我不确定您是否仍然可以使用$ data-&gt; ca-&gt; date,因为您的数据不是已知的活动记录类型。
尝试放
$criteria->select = 'maintable.*,ca.date as ca_date';
然后你应该可以使用
array(
'header' => 'Last Activity',
'class' => 'gridDataColumn',
'value' => '$data->ca_date',
),
答案 1 :(得分:0)
下面是模型,它将允许具有SORTABLE的自定义/计算字段。而不是简单地写下最后活动&#39; =&gt;&#39; lastactivity&#39;在排序数组中,传递整个数组对我来说很有用。希望它可以帮助某人:)
类联系人扩展了CActiveRecord {
public $verifyCode;
public $lastactivity;
public static function model($className = __CLASS__) {
return parent::model($className);
}
public function tableName() {
return '{{contact}}';
}
public function rules() {
return array(
array('name, email, subject, message', 'required', 'message' => Yii::t('app', 'MSG_ATTRIBUTE_BLANK')),
array('email', 'email'),
array('verifyCode', 'CaptchaExtendedValidator', 'allowEmpty' => !CCaptcha::checkRequirements(), 'on' => 'fContact'),
array('name, email,subject,message,lastactivity', 'safe', 'on' => 'search'),
array('name, subject, email, message, status,createdon,updatedon,verifyCode,lastactivity', 'safe'),
);
}
public function relations() {
return array(
'ca' => array(self::HAS_MANY, 'ContactAction', 'contact_id'),
);
}
public function search() {
$criteria = new CDbCriteria;
$criteria->compare('name', CommonFunctions::escapeOperator($this->name), true, 'OR');
$criteria->compare('subject', CommonFunctions::escapeOperator($this->subject), true, 'OR');
$criteria->compare('email', CommonFunctions::escapeOperator($this->email), true, 'OR');
$criteria->compare('status', CommonFunctions::escapeOperator($this->status), true,'OR');
$lastactivity_sql = '(select max(date) from tbl_contact_action ca where ca.contact_id=t.contact_id)';
$criteria->select = array('*', $lastactivity_sql . ' as lastactivity');
$criteria->addCondition('status<>"Deleted"');
$criteria->compare($lastactivity_sql, $this->lastactivity);
$sort = new CSort;
$sort->defaultOrder = array('lastactivity' => CSort::SORT_DESC); //'title ASC';
$sort->attributes = array(
'name' => 'name',
'email' => 'email',
'status' => 'status',
'createdon' => 'createdon',
'lastactivity' => array(
'asc' => 'lastactivity ASC',
'desc' => 'lastactivity DESC',
),
);
$sort->applyOrder($criteria);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'sort' => $sort,
'pagination' => array(
'pageSize' => Yii::app()->user->getState('contactPageSize', Yii::app()->params['RECORDPERPAGE_ADMIN']),
'currentPage' => Yii::app()->user->getState('Contact_page', 0),
),
));
}
}