【发布时间】:2019-09-05 08:23:52
【问题描述】:
我有线程和线程上的消息
我想返回最后一条消息时间的所有线程,所以我在线程模型上添加了一个这样的新字段
public function fields()
{
$fields= ['idThread', 'idUser', 'title', 'unread', 'username','lastMesageTime'];
return $fields;
}
现在用这个方法我得到了 lastMessageTime 的计算值
public function getLastMessageTime()
{
return $this->hasMany(Messages::className(), ['idThread' => 'idThread'])
->select('time')->orderBy('time DESC')->limit(1)->scalar();
}
在我的索引方法上使用这样的活动记录
return Thread::find()->select('idThread, title, idUser')->all();
这行得通,我得到了具有正确值的 lastMessageTime,但我想按顺序排序,这样我就可以得到第一个具有最新 lastMessageTime 的线程,我尝试使用以下代码
public function scopes() {
return array(
'byOrden' => array('order' => 'lastTimeMessage DESC'),
);
}
有什么想法吗?
编辑: 这种解决方法有效,但我认为这不是一个好方法,因为我没有使用活动记录,所以我在线程模型上定义的用户名等字段我不得不再次获取它
$query = (new \yii\db\Query());
$query->select('*, (SELECT max(time) as lastMessageTime from messages where messages.idThread = thread.idThread ) lastMessageTime,
(SELECT name from users where users.idUser = thread.idUser) as name ')
->from('threads')
->where(['idUser'=>$idUser])
->orderBy('lastMessageTime DESC');
$rows = $query->all();
return $rows;
【问题讨论】:
标签: database activerecord yii2