【问题标题】:ember data not saving foreign key, sent as nullember 数据不保存外键,作为 null 发送
【发布时间】:2015-11-04 19:43:49
【问题描述】:

我的 ember 应用没有将我的外键发送到后端。

我有一个名为 issues 的表,其中有一个名为 categories 的相关表

我的模型是:

import DS from 'ember-data';

export default DS.Model.extend({
    name:           DS.attr('string'),
    category_id:    DS.belongsTo('category'),
    description:    DS.attr('string')
});

我的路线是:

import Ember from 'ember';

export default Ember.Route.extend({
  model: function(){
    return this.store.findAll('issue');
  },
  actions: {
    create: function(){

        var issue = this.store.createRecord('issue');
        issue.name = this.get('controller').get('newName');
        issue.description = this.get('controller').get('newDescription');
        issue.category_id = parseInt(this.get('controller').get('newCategory'));
        //debugger;

        console.log(issue);
        issue.save();
    },
    ...
    other actions 
    ...
    }
  }
});

上面的 console.log 看起来 category_id 设置正确:

category_id: 3
description: "foobar"
name: "test"

但是,我发送到后端的 JSON 负载看起来像:

{"issue":{"name":"test","description":"foobar","category_id":null}}

我尝试通过在 app/serializers/application.js 中添加自定义序列化程序来逐步完成

export default DS.RESTSerializer.extend({
  ...

    serialize: function(snapshot,options){
      console.debug('options='+options);
      debugger;
      var json = this._super(snapshot, options);;

      return json;
    }
  ...
  });

但是我迷失在所有的超级调用超级间接中。

snapshot.recordcategory_id: 3,但从 this._super() 调用返回的 jsoncategory_id: null

选项includeID:true

任何线索将不胜感激......

余烬:2.0.2
灰烬数据:2.0.0

【问题讨论】:

    标签: ember.js foreign-keys ember-data


    【解决方案1】:

    你的模型定义是错误的,在处理关系时你定义它们就像你定义任何其他属性一样,没有必要使用_id

    export default DS.Model.extend({
        name:           DS.attr('string'),
        category:    DS.belongsTo('category'),
        description:    DS.attr('string')
    });
    

    至于创建,在处理 ember 对象时应该始终使用 setter/getter:

    create: function() {
      var issue = this.store.createRecord('issue', {
        name: this.get('controller').get('newName'),
        description: this.get('controller').get('newDescription'),
        category: this.get('controller').get('newCategory') // assuming new category is a DS.Model instance of category
      });
      issue.save();
    }
    

    如果您希望坚持使用 issue.set('name', this.get('controller').get('newName')) 的语法,从您的代码的外观来看,您似乎以错误的方式处理此问题。

    您应该有一个 this.route('new') 嵌套在您的问题路由下,这样您就不必使用控制器来存储信息。

    您只需将新路线的模型设置为:

    model: function() {
      return this.store.createRecord('issue');
    }
    

    您的模板会像这样使用input helpers

    {{input value=model.name}},您的操作只会得到currentModel 并调用.save()

    【讨论】:

    • 我最初使用的是类别(没有 _id),但我的后端 API 使用了 _id,我在序列化程序中添加了 keyForRelationship,这对于检索记录很好,但我不确定是否它正在用于存储,所以我试图检查是否是导致问题的原因。
    • 我在索引模板的顶部有我的输入,它应该允许用户添加新问题,我不确定如何正确构建它,如果我有问题/索引/新文件夹(我正在使用 pod)
    • 你的后端有任何机会吗?
    • 后端是 php lumen(基于 laravel)
    猜你喜欢
    • 1970-01-01
    • 2017-02-23
    • 2015-12-02
    • 2016-08-18
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    相关资源
    最近更新 更多