1

「withRelated」を使用してbookshelfjsでクエリを実行している2つのテーブルがあります。関連テーブルの列で結果を並べ替えるにはどうすればよいですか? たとえば、すべての結果を並べ替えたいrankingLinks.id

.fetchAll({withRelated: ['tickerSymbolLinks.tickerSymbol', 'rankingLinks', 'logoLinks.logo']})

シンプルに思えますが、これで壁に頭をぶつけます...

4

1 に答える 1

3

実際にはwithRelatedで並べることができますが、この特定のプロパティ内の結果のみを並べ替えます。すなわち:

new Participant().query(function(qb) {
    qb.orderBy('participants.id', 'desc'); // You don't have access to any "withRelated" models
}).fetchAll({
    withRelated: [{
        'courses': function(qb) {
            qb.orderBy('courses.id', 'desc');
        }
    }]
}).then(function(result) {
    res.json(result.toJSON());
});

このコードは、ID 順 (降順) にすべての参加者を返します。この結果の中で、courses.id dess でコースを並べ替えます。結果は次のとおりです。

[
     {
          "id": 3,
          "name": "Susan Doe",
          "created_at": 1429685077496,
          "updated_at": 1429685077496,
          "courses": [
               {
                    "id": 3,
                    "course_name": "Advanced JavaScript",
                    "duration": 80,
                    "_pivot_participant_id": 3,
                    "_pivot_course_id": 3,
                    "_pivot_level": null,
                    "_pivot_grade": null,
                    "_pivot_date_of_attendance": null
               },
               {
                    "id": 1,
                    "course_name": "iOS development",
                    "duration": 120,
                    "_pivot_participant_id": 3,
                    "_pivot_course_id": 1,
                    "_pivot_level": null,
                    "_pivot_grade": null,
                    "_pivot_date_of_attendance": null
               }
          ]
     },
     {
          "id": 2,
          "name": "Richard Doe",
          "created_at": 1429685077496,
          "updated_at": 1429685077496,
          "courses": [
               {
                    "id": 2,
                    "course_name": "Android development",
                    "duration": 60,
                    "_pivot_participant_id": 2,
                    "_pivot_course_id": 2,
                    "_pivot_level": null,
                    "_pivot_grade": null,
                    "_pivot_date_of_attendance": null
               },
               {
                    "id": 1,
                    "course_name": "iOS development",
                    "duration": 120,
                    "_pivot_participant_id": 2,
                    "_pivot_course_id": 1,
                    "_pivot_level": null,
                    "_pivot_grade": null,
                    "_pivot_date_of_attendance": null
               }
          ]
     },
     {
          "id": 1,
          "name": "John Doe",
          "created_at": 1429685077495,
          "updated_at": 1429685077495,
          "courses": [
               {
                    "id": 3,
                    "course_name": "Advanced JavaScript",
                    "duration": 80,
                    "_pivot_participant_id": 1,
                    "_pivot_course_id": 3,
                    "_pivot_level": null,
                    "_pivot_grade": null,
                    "_pivot_date_of_attendance": null
               },
               {
                    "id": 1,
                    "course_name": "iOS development",
                    "duration": 120,
                    "_pivot_participant_id": 1,
                    "_pivot_course_id": 1,
                    "_pivot_level": null,
                    "_pivot_grade": null,
                    "_pivot_date_of_attendance": null
               }
          ]
     }
]

結果をコース別に並べ替えたい場合は、クエリを少し変更する必要があります。すなわち:

new Course().query(function(qb) {
    qb.orderBy('courses.id', 'desc');
}).fetchAll({
    withRelated: ['participants']
}).then(function(result) {
    res.json(result.toJSON());
});

これにより、わずかに異なる結果が生成されますが、望ましい順序になります。

[
     {
          "id": 3,
          "course_name": "Advanced JavaScript",
          "duration": 80,
          "participants": [
               {
                    "id": 1,
                    "name": "John Doe",
                    "created_at": 1429685077495,
                    "updated_at": 1429685077495,
                    "_pivot_course_id": 3,
                    "_pivot_participant_id": 1,
                    "_pivot_level": null,
                    "_pivot_grade": null,
                    "_pivot_date_of_attendance": null
               },
               {
                    "id": 3,
                    "name": "Susan Doe",
                    "created_at": 1429685077496,
                    "updated_at": 1429685077496,
                    "_pivot_course_id": 3,
                    "_pivot_participant_id": 3,
                    "_pivot_level": null,
                    "_pivot_grade": null,
                    "_pivot_date_of_attendance": null
               }
          ]
     },
     {
          "id": 2,
          "course_name": "Android development",
          "duration": 60,
          "participants": [
               {
                    "id": 2,
                    "name": "Richard Doe",
                    "created_at": 1429685077496,
                    "updated_at": 1429685077496,
                    "_pivot_course_id": 2,
                    "_pivot_participant_id": 2,
                    "_pivot_level": null,
                    "_pivot_grade": null,
                    "_pivot_date_of_attendance": null
               }
          ]
     },
     {
          "id": 1,
          "course_name": "iOS development",
          "duration": 120,
          "participants": [
               {
                    "id": 1,
                    "name": "John Doe",
                    "created_at": 1429685077495,
                    "updated_at": 1429685077495,
                    "_pivot_course_id": 1,
                    "_pivot_participant_id": 1,
                    "_pivot_level": null,
                    "_pivot_grade": null,
                    "_pivot_date_of_attendance": null
               },
               {
                    "id": 2,
                    "name": "Richard Doe",
                    "created_at": 1429685077496,
                    "updated_at": 1429685077496,
                    "_pivot_course_id": 1,
                    "_pivot_participant_id": 2,
                    "_pivot_level": null,
                    "_pivot_grade": null,
                    "_pivot_date_of_attendance": null
               },
               {
                    "id": 3,
                    "name": "Susan Doe",
                    "created_at": 1429685077496,
                    "updated_at": 1429685077496,
                    "_pivot_course_id": 1,
                    "_pivot_participant_id": 3,
                    "_pivot_level": null,
                    "_pivot_grade": null,
                    "_pivot_date_of_attendance": null
               }
          ]
     }
]

これが少し役立つことを願っています!

于 2015-04-22T06:46:39.437 に答える