[Bugfix] take the correct localField when having a manyToOne RS

This commit is contained in:
Kamal Bennani 2018-09-22 21:45:22 +02:00
parent 382f52a2e8
commit 83da9fa38a
No known key found for this signature in database
GPG Key ID: 4513063CDB1A1C25
2 changed files with 18 additions and 10 deletions

View File

@ -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
});

View File

@ -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') :