bookshelf.js,在相关表上查询



我的模型类似于以下:

var ScholarlyPaper = Bookshelf.Model.extend({
  tableName: 'papers',
  paragraphs: function() {
    return this.hasMany(Paragraph).through(Section);
  },
  sections: function() {
    return this.hasMany(Section);
  }
});
var Section = Bookshelf.Model.extend({
  tableName: 'sections',
  paragraphs: function() {
    return this.hasMany(Paragraph);
  }
  scholarlyPaper: function() {
    return this.belongsTo(ScholarlyPaper);
  }
});
var Paragraph = Bookshelf.Model.extend({
  tableName: 'paragraphs',
  section: function() {
    return this.belongsTo(Section);
  },
  scholarlyPaper: function() {
    return this.belongsTo(ScholarlyPaper).through(Section);
  },
  author: function() {
    return this.belongsTo(Author);
  }
});
var Author = Bookshelf.Model.extend({
  tableName: 'authors',
  paragraphs: function() {
    return this.hasMany(Paragraph);
  }
});

使用bookshelf.js,给定一个学术纸ID和作者ID,我如何在论文中获取作者没有在?

中写下单段

我面临的特殊挑战是,我没有办法在相关表上添加一个子句(例如paragraphs.author_id!= furety_id)。

此起作用吗?

new ScholarlyPaper({id: 1}).load({paragraphs: function(qb) {
  qb.where('paragraphs.author_id', '!=', author_id);
}}).then(function(paper) {
  console.log(JSON.stringify(paper.related('paragraphs')));
});
function(authorId, paperId, success, failure) {
  new ScholarlyPaper({id: paperId}).load({sections: function(qb) {
    qb.whereNotExists(function() {
      this.from('paragraph')
        .whereRaw('paragraph.section = section.id')
        .where('paragraph.author_id', '=', authorId);
    });
  }}).then(function(paper) {
    success(paper.related('section'));
  }, failure);
};

查看书架 - 口语扩展。WhereHas()和()函数可能是您要寻找的。您的功能看起来像这样:

async function(authorId, paperId) {
    return await ScholarlyPaper.where('id', paperId)
        .with('sections', (q) {
            // Filter out sections in the paper that the author did not write a single paragraph in.
            q.whereHas('paragraphs', (q) => {
                q.where('author_id', authorId);
            }, '<=', 0);
        }).first();
}

相关内容

  • 没有找到相关文章

最新更新