我想知道在emberfire中创造多对多的关系是否可能。 我有以下型号:
//Employee
name: DS.attr('string'),
position: DS.attr('string'),
accessoryPosition: DS.attr('string'),
education: DS.attr('string'),
experience: DS.attr('string'),
imgUrl: DS.attr('string'),
teachingIn: DS.attr('string'),
subjects: DS.hasMany('subject', {async: true})
//Subject
name: DS.attr('string'),
courseDescriptionUrl: DS.attr('string'),
description: DS.attr('string'),
examDescriptionUrl: DS.attr('string'),
imgUrl: DS.attr('string'),
sportsSubject: DS.attr('boolean'),
outdoorSubject: DS.attr('boolean'),
commonSubject: DS.attr('boolean'),
teachers: DS.hasMany('employee', {async : true})
然后在我的员工控制器中:
update(subjects) {
this.get('model.employee').set('subjects',subjects);
this.get('model.employee').save();
}
但是他只将它添加到我的/ employees端点。无论如何要让这种关系相互促进吗?
答案 0 :(得分:0)
您必须手动将工作添加到主题。一种方法是这样的:
update(subjects) {
this.get('model.employee').set('subjects', subjects);
this.get('model.employee').save();
subjects.forEach(subject => {
subject.get('teachers').addObject(subject);
});
subjects.invoke('save');
}
Array#pushObject
添加subject
如果它已经不在数组中,并且Array#invoke
会在数组的每个元素上调用save
方法。
希望这有用。