保存与余烬数据的parent_id关系 [英] Saving parent_id relationship with ember data
问题描述
好。我有一个真正的好看的谷歌和其他来源谷歌返回,但我还没有找到一个答案我的问题。
Okay. I've had a real good look through SO and other sources returned by Google, but am yet to find an answer to my problem.
我有两个模型: p>
I have two models:
App.Kid = Ember.Model.extend
title: DS.attr "string"
parent: DS.belongsTo "App.Parent"
App.Parent = Ember.Model.extend
title: DS.attr "string"
kids: DS.hasMany "App.Kid"
这里讨论的大多数问题都是讨论从侧面加载的JSON数据中检索垃圾数据关系,我有能力做。我需要知道的是如何在创建一个新的孩子时保存parent_id?
Most questions on here discuss retrieving ember data relationships from sideloaded JSON data, which I'm capable of doing. What I need to know is how do I save the parent_id when creating a new kid?
目前我正在App.KidController中保存一个新的孩子:
At the moment I'm saving a new kid in the App.KidController like so:
App.ParentController = Ember.ObjectController.extend
needs: [ "widgets" ]
App.KidCreateController = Ember.ObjectController.extend
needs: [ "dashboard" ]
saveChild: ->
@content.get( "store" ).commit()
当保存时没有parent_id被看到,但是我得到一个新的父对象(分配给父键),ID为0。
And when saving there is no parent_id to be seen, however I do get a new parent object (assigned to a parent key) with the id of 0.
任何人都可以解释我是在这里做错?
Can anybody explain what I'm doing wrong here?
更新1
所以使用我的实际情况我'将解释我在做什么。
So using my actual situation I'll explain what I'm doing.
用户有几个仪表板,其中附有多个小部件。我的模型结构如下所示:
A user has several dashboards which have multiple widgets attached to it. My model structure looks like this:
App.Dashboard = DS.Model.extend
title: DS.attr "string"
widgets: DS.hasMany "App.Widget"
App.Widget = DS.Model.extend
title: DS.attr "string"
type: DS.attr "string"
...
dashboard: DS.belongsTo "App.Dashboard"
推荐答案
嗯,原来我是个白痴。我使用Laravel作为我的后端,当定义你的模型时,你需要定义哪些属性是可填充的,但我忘了添加dashboard_id到列表中。一切都正常。
Well, it turns out I was being a idiot. I'm using Laravel as my backend and when defining your models you need to define which attributes are fillable, however I forgot to add dashboard_id to the list. Everything works fine now.
这篇关于保存与余烬数据的parent_id关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!