From 83da9fa38a87d82f18de5723bd16da6c399b709e Mon Sep 17 00:00:00 2001 From: Kamal Bennani Date: Sat, 22 Sep 2018 21:45:22 +0200 Subject: [PATCH] [Bugfix] take the correct localField when having a manyToOne RS --- .../templates/bookshelf/service.template | 19 +++++++++++-------- .../templates/mongoose/service.template | 9 +++++++-- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/packages/strapi-generate-api/templates/bookshelf/service.template b/packages/strapi-generate-api/templates/bookshelf/service.template index 4b67cac521..aac20bb6cd 100644 --- a/packages/strapi-generate-api/templates/bookshelf/service.template +++ b/packages/strapi-generate-api/templates/bookshelf/service.template @@ -40,17 +40,13 @@ module.exports = { } }); - if (filters.sort) { - qb.orderBy(filters.sort.key, filters.sort.order); - } - Object.keys(filters.relations).forEach( (relationName) => { const ast = <%= globalID.toLowerCase() %>.associations.find(a => a.alias === relationName); if (ast) { const model = ast.plugin ? - strapi.plugins[ast.plugin].models[ast.model ? ast.model : ast.collection] : - strapi.models[ast.model ? ast.model : ast.collection]; + strapi.plugins[ast.plugin].models[ast.model || ast.collection] : + strapi.models[ast.model || ast.collection]; qb.distinct(); @@ -70,8 +66,11 @@ module.exports = { const externalKey = ast.type === 'collection' ? `${model.collectionName}.${ast.via}` : `${model.collectionName}.${model.primaryKey}`; - const internalKey = !ast.dominant ? `${<%= globalID.toLowerCase() %>.collectionName}.${<%= globalID.toLowerCase() %>.primaryKey}` : - ast.via === <%= globalID.toLowerCase() %>.collectionName ? `${<%= globalID.toLowerCase() %>.collectionName}.${ast.alias}` : `${<%= globalID.toLowerCase() %>.collectionName}.${<%= globalID.toLowerCase() %>.primaryKey}`; + const internalKey = !ast.dominant + ? `${<%= globalID.toLowerCase() %>.collectionName}.${<%= globalID.toLowerCase() %>.primaryKey}` + : ast.via === <%= globalID.toLowerCase() %>.collectionName + ? `${<%= globalID.toLowerCase() %>.collectionName}.${ast.alias}` + : `${<%= globalID.toLowerCase() %>.collectionName}.${<%= globalID.toLowerCase() %>.primaryKey}`; qb.innerJoin(relationTable, externalKey, internalKey); } @@ -88,6 +87,10 @@ module.exports = { qb.offset(filters.start); qb.limit(filters.limit); + + if (filters.sort) { + qb.orderBy(filters.sort.key, filters.sort.order); + } }).fetchAll({ withRelated: populate }); diff --git a/packages/strapi-generate-api/templates/mongoose/service.template b/packages/strapi-generate-api/templates/mongoose/service.template index b3fc91db8e..b96c0fd9c0 100644 --- a/packages/strapi-generate-api/templates/mongoose/service.template +++ b/packages/strapi-generate-api/templates/mongoose/service.template @@ -26,9 +26,14 @@ module.exports = { const populate = <%= globalID %>.associations .filter(ast => ast.autoPopulate) .reduce((acc, ast) => { - const from = ast.plugin ? `${ast.plugin}_${ast.model}` : ast.collection ? ast.collection : ast.model; + // Strapi Model + const model = ast.plugin + ? strapi.plugins[ast.plugin].models[ast.collection || ast.model] + : strapi.models[ast.collection || ast.model]; + + const from = model.collectionName; const as = ast.alias; - const localField = !ast.dominant ? '_id' : ast.via === <%= globalID %>.collectionName || ast.via === 'related' ? '_id' : ast.alias; + const localField = ast.dominant ? '_id' : ast.via === <%= globalID %>.collectionName || ast.via === 'related' ? '_id' : ast.alias; const foreignField = ast.filter ? `${ast.via}.ref` : ast.dominant ? (ast.via === <%= globalID %>.collectionName ? ast.via : '_id') :