我似乎在从相关模型中检索属性时遇到了问题。
型号:
public sealed partial class MainPage : Page
{
private ObservableCollection<string> myList = new ObservableCollection<string> { "1", "2", "3", "4", "5" };
private int i = 6;
public MainPage()
{
this.InitializeComponent();
MyGridView.ItemsSource = myList;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
myList.Add(i.ToString());
i += 1;
}
}
管理员注册路线:
// DB = export of Bookshelf
// Signup model
var DB = require('../db').DB;
var UserMeta = require('./usersmeta');
var Signup = DB.Model.extend({
tableName: 'tblSignups',
idAttribute: 'id',
hasTimestamps: true,
usermeta: function() {
return this.belongsTo(UserMeta);
}
});
module.exports = Signup;
// Usermeta model
var DB = require('../db').DB;
var User = require('./users');
var UserMeta = DB.Model.extend({
tableName: 'tblUsersMeta',
idAttribute: 'id',
hasTimestamps: true,
user: function() {
return this.belongsTo(User);
}
});
module.exports = UserMeta;
这是控制台日志显示的内容:
router.get('/signups', function(req, res, next) {
model.SignUp.fetchAll({withRelated: ['usermeta']}).then(function(collection) {
for(var i = 0; i < collection.toArray().length; i++){
console.log('Signup ' + i + '\n--------------------------');
console.log(collection.toArray()[i]);
console.log('Related usermeta\n--------------------------');
console.log(collection.toArray()[i].related('usermeta'));
}
res.render('admin/pages/signups', {title: 'signups', error: false, signups: collection.toArray()});
}).catch(function(err) {
res.status(500).json({error: true, data: {message: err.message}});
});
});
因此,您可以看到相关的模型属性为空。我保存了我的usermeta,如下所示:
Signup 0
--------------------------
ModelBase {
attributes:
{ id: 2,
usermeta_id: 5,
state: 'pending',
created_at: Thu May 05 2016 17:21:13 GMT+0200 (Romance Daylight Time),
updated_at: Thu May 05 2016 17:21:13 GMT+0200 (Romance Daylight Time) },
_previousAttributes:
{ id: 2,
usermeta_id: 5,
state: 'pending',
created_at: Thu May 05 2016 17:21:13 GMT+0200 (Romance Daylight Time),
updated_at: Thu May 05 2016 17:21:13 GMT+0200 (Romance Daylight Time) },
changed: {},
relations:
{ usermeta:
ModelBase {
attributes: {},
_previousAttributes: {},
changed: {},
relations: {},
cid: 'c38',
relatedData: [Object] } },
cid: 'c35',
id: 2 }
Related usermeta
--------------------------
ModelBase {
attributes: {},
_previousAttributes: {},
changed: {},
relations: {},
cid: 'c38',
relatedData:
RelationBase {
targetTableName: 'tblUsersMeta',
targetIdAttribute: 'id',
type: 'belongsTo',
target:
{ [Function]
NotFoundError: [Function: ErrorCtor],
NoRowsUpdatedError: [Function: ErrorCtor],
NoRowsDeletedError: [Function: ErrorCtor] },
foreignKey: 'tblUsersMetum_id',
parentId: undefined,
parentTableName: 'tblSignups',
parentIdAttribute: 'id',
parentFk: undefined } }
在数据库中,usermeta的id(在注册表中)设置正确。我在这里做错了什么?
答案 0 :(得分:1)
您的模型似乎有问题。当您在两个示例中使用belongsTo
建立模型之间的关系时 - 这可能是正确的。我不知道它们之间的关系是什么(多对多?一对多?)。如果是多对多,则需要使用belongsToMany
,如果是一对多,则需要在不支持外国人的模型上设置hasMany
键入数据库,belongsTo
键入数据库中具有外键的模型。
请参阅此http://bookshelfjs.org/#one-to-many和此http://bookshelfjs.org/#many-to-many
希望这有帮助!