Merge pull request #933 from strapi/poly-cm

Handle polymorphic in content manager
This commit is contained in:
Jim LAURIE 2018-04-13 10:14:23 +02:00 committed by GitHub
commit bae40c837d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 14 deletions

View File

@ -30,8 +30,9 @@ function EditRelations(props) {
{(message) => <h3>{message}</h3>}
</FormattedMessage>
{map(filterRelationsUpload(props.schema.relations), (relation, key) => {
if (relation.nature.toLowerCase().includes('morph') && relation[key]) return '';
const Select = ['oneWay', 'oneToOne', 'manyToOne'].includes(relation.nature) && relation.dominant ? SelectOne : SelectMany;
const Select = ['oneWay', 'oneToOne', 'manyToOne', 'oneToManyMorph', 'oneToOneMorph'].includes(relation.nature) && relation.dominant ? SelectOne : SelectMany;
return (
<Select

View File

@ -223,7 +223,8 @@ export class EditPage extends React.Component {
isRelationComponentNull = () => (
Object.keys(get(this.getSchema(), 'relations', {})).filter(relation => (
get(this.getSchema(), ['relations', relation, 'plugin']) !== 'upload'
get(this.getSchema(), ['relations', relation, 'plugin']) !== 'upload' &&
(!get(this.getSchema(), ['relations', relation, 'nature'], '').toLowerCase().includes('morph') || !get(this.getSchema(), ['relations', relation, relation]))
)).length === 0
)

View File

@ -14,13 +14,14 @@ module.exports = {
.count());
},
findOne: async function (params, populate) {
return this
findOne: async function (params, populate, raw = true) {
const query = this
.findOne({
[this.primaryKey]: params[this.primaryKey] || params.id
})
.populate(populate || this.associations.map(x => x.alias).join(' '))
.lean();
.populate(populate || this.associations.map(x => x.alias).join(' '));
return raw ? query.lean() : query;
},
create: async function (params) {
@ -300,18 +301,18 @@ module.exports = {
However the upload doesn't need this method. It only uses the `removeRelationMorph`.
*/
const entry = await module.exports.findOne.call(this, params, []);
const value = entry[params.alias] || [];
const entry = (await module.exports.findOne.call(this, params, [], false)).toJSON();
const value = [];
// Retrieve association.
const association = this.associations.find(association => association.via === params.alias)[0];
const association = this.associations.find(association => association.alias === params.alias);
if (!association) {
throw Error(`Impossible to create relationship with ${params.ref} (${params.refId})`);
}
// Resolve if the association is already existing.
const isExisting = entry[params.alias].find(obj => {
const isExisting = value.find(obj => {
if (obj.kind === params.ref && obj.ref.toString() === params.refId.toString() && obj.field === params.field) {
return true;
}
@ -326,9 +327,10 @@ module.exports = {
// Push new relation to the association array.
value.push({
ref: params.refId,
ref: params.ref,
refId: params.refId,
kind: params.ref,
field: association.filter
field: params.field
});
entry[params.alias] = value;

View File

@ -59,7 +59,7 @@ module.exports = {
});
// Then, request plugin upload.
if (strapi.plugins.upload) {
if (strapi.plugins.upload && !_.isEmpty(values.files)) {
// Upload new files and attach them to this entity.
await strapi.plugins.upload.services.upload.uploadToEntity({
id: entry.id || entry._id,

View File

@ -40,4 +40,4 @@
"configurable": false
}
}
}
}