Bookshelfjs:如何根据另一个表的信息获取所有条目

时间:2016-06-03 06:55:05

标签: mysql bookshelf.js

我有一张userscourses的表格(例如:心理学101,poli sci 205等)。我还有一个名为users_courses的连接表。我想获取属于用户的所有课程。

我有两个与解决这个问题有关的问题:

  1. 我应该在模型或集合上使用fetchAll()course vs courses)。有什么区别?
  2. 在书架中,如何引用属于特定用户的所有课程?

1 个答案:

答案 0 :(得分:1)

  1. "-KJSKE4a2y-okl71vDSx": { Interest = men; gender = female; name = girlsname1; photo = "http://i.imgur.com/VAWlQ0S.gif"; surname = girlsurname1; }, "-KJSKE4b8TlvxV-urHQo": { Interest = men; gender = female; name = girlsname6; photo = "http://media.tumblr.com/tumblr_lnb9aozmM71qbxrlp.gif"; surname = girlsurname6; 是一个模型函数(参见fetchAll)。它会根据查询参数带来一系列模型,在事件发生时触发fetchAll()'fetching'事件。

  2. 相比'fetched'只带来当前模型(或匹配当前设置属性的模型),如:

    fetch()
  3. 对于这个例子,我假设你的模型是:

    new User({name: 'Mary Jane'})
      .fetch({withRelated: 'courses'})
      .then(function(mary) {
         console.log('Mary Jane and her courses: ' + mary.toJSON());
      });