ORM续集可以有多态性吗?

Ach*_*les 4 orm

我有一个名为User的模型。我可以从用户模型扩展其他模型Admin / mod吗?我找到了续集文档,但我没有找到

Ale*_*emi 5

是的,请查阅Associations文档,或者在Scopes文档中更全面地查看。

从文档:

this.Comment = this.sequelize.define('comment', {
  title: Sequelize.STRING,
  commentable: Sequelize.STRING,
  commentable_id: Sequelize.INTEGER
});

this.Comment.prototype.getItem = function() {
  return this['get' + this.get('commentable').substr(0, 1).toUpperCase() + this.get('commentable').substr(1)]();
};

this.Post.hasMany(this.Comment, {
  foreignKey: 'commentable_id',
  constraints: false,
  scope: {
    commentable: 'post'
  }
});
this.Comment.belongsTo(this.Post, {
  foreignKey: 'commentable_id',
  constraints: false,
  as: 'post'
});

this.Image.hasMany(this.Comment, {
  foreignKey: 'commentable_id',
  constraints: false,
  scope: {
    commentable: 'image'
  }
});
this.Comment.belongsTo(this.Image, {
  foreignKey: 'commentable_id',
  constraints: false,
  as: 'image'
});
Run Code Online (Sandbox Code Playgroud)

  • 是否可以使用Commentable.find()函数获取“可注释”数据(例如Post或Image的行数据)? (2认同)