Merge branch 'master' into fix-cm-entry-relations

This commit is contained in:
Jim LAURIE 2018-05-02 17:04:41 +02:00 committed by GitHub
commit e6a14367f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 3 deletions

View File

@ -236,6 +236,7 @@ module.exports = function(strapi) {
const association = definition.associations
.filter(association => association.nature.toLowerCase().indexOf('morph') !== -1)
.filter(association => association.alias === path || association.via === path)[0];
if (association) {
// Override on polymorphic path only.
if (_.isString(path) && path === association.via) {
@ -252,7 +253,11 @@ module.exports = function(strapi) {
strapi.plugins[association.plugin].models[association.collection || association.model]:
strapi.models[association.collection || association.model];
return `${association.alias}.${model.collectionName}`;
return {
[`${association.alias}.${model.collectionName}`]: function(query) {
query.orderBy('created_at', 'desc');
}
};
}
}

View File

@ -218,6 +218,28 @@ module.exports = {
strapi.plugins[obj.source].models[obj.ref]:
strapi.models[obj.ref];
// Remove existing relationship because only one file
// can be related to this field.
if (association.nature === 'manyMorphToOne') {
return virtualFields.push(
module.exports.removeRelationMorph.call(this, {
alias: association.alias,
ref: model.collectionName,
refId: obj.refId,
field: obj.field
})
.then(() =>
module.exports.addRelationMorph.call(this, {
id: response[this.primaryKey],
alias: association.alias,
ref: model.collectionName,
refId: obj.refId,
field: obj.field
})
)
);
}
virtualFields.push(module.exports.addRelationMorph.call(this, {
id: response[this.primaryKey],
alias: association.alias,
@ -386,12 +408,12 @@ module.exports = {
removeRelationMorph: async function (params) {
return await this.morph.forge()
.where({
.where(_.omitBy({
[`${this.collectionName}_id`]: params.id,
[`${params.alias}_id`]: params.refId,
[`${params.alias}_type`]: params.ref,
field: params.field
})
}, _.isEmpty))
.destroy();
}
};