Saturday, 15 January 2011

node.js - Return model where the pivot entry exists -


I am using bookshelfJS I have two models users and posted Post_User .

Now, a user_id is given, I want to find all the posts for that user, so far I have to call it knex :

  knex.select () has managed to use from .from ('post_user'). Where ('user_id', user_id) Then (function (Pivots) {// now loop through Pipes and return all posts // pivot.post_id using pivots.map (function (pivot) {})});  

Is there a cleaner way to do this?

You want to define many-to-many relationships in your bookshelf model something like this:

  var user = bookshelf.model.extend ({tableName: 'user', post: function ()} return to this.Belongtaman (post);}}); Var post = bookshelf.model.extend ({tableName: 'post', user: function ()} return to this.Belgstatman (user);}});  

From the conference, the Pivot Table bookhelf will be used with Post_user ( _ combined with the table name, starting with the table alphabetically first). There should be a user_id and a post_id (and one of two copposts PK). If you do not want to change the name of the pivot table, look for instructions for defining the table and your pivot table ID.

After this, you can ask your model with a bookcase:

Also see.


No comments:

Post a Comment