这里我有一个带密码的事件字段。我需要通过download_count DESC订购所有密码。所以我试过下面的代码。它没有给我任何错误,但是desc无法正常工作。
$event = $this->Events->get($id, [
'contain' => [
'EventPasswordAll.Passwords' => [
'queryBuilder' => function (Query $q) {
return $q->order(['download_count' => 'DESC']);
}
]
]
]);
我也试过
return $q->order(['Passwords.download_count' => 'DESC']);
我收到了查询
SELECT EventPasswordAll.id AS `EventPasswordAll__id`, EventPasswordAll.event_id AS `EventPasswordAll__event_id`, EventPasswordAll.password_id AS `EventPasswordAll__password_id`, Passwords.id AS `Passwords__id`, Passwords.name AS `Passwords__name`, Passwords.collection_id AS `Passwords__collection_id`, Passwords.photo_count AS `Passwords__photo_count`, Passwords.download_count AS `Passwords__download_count`, Passwords.created AS `Passwords__created`, Passwords.expires_at AS `Passwords__expires_at`, Passwords.recommended AS `Passwords__recommended`, Passwords.accepted AS `Passwords__accepted`, Passwords.user_id AS `Passwords__user_id` FROM event_password_all EventPasswordAll INNER JOIN passwords Passwords ON Passwords.id = (EventPasswordAll.password_id) WHERE EventPasswordAll.event_id in (14)
如何在queryBuilder中为关联数据添加顺序?
答案 0 :(得分:1)
使用EventPasswordAll belongsTo Passwords
的关联,将通过连接检索passwords
表记录,如查询中所示。在这种情况下,没有Passwords
的查询,因此不会调用查询构建器。
您必须设置EventPasswordAll
查询构建器的顺序,例如
$event = $this->Events->get($id, [
'contain' => [
'EventPasswordAll' => [
'queryBuilder' => function (Query $q) {
return $q->order(['Passwords.download_count' => 'DESC']);
},
'Passwords'
]
]
]);