Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attach with additional attributes #29

Closed
vjpr opened this issue Jun 20, 2013 · 8 comments
Closed

attach with additional attributes #29

vjpr opened this issue Jun 20, 2013 · 8 comments

Comments

@vjpr
Copy link

vjpr commented Jun 20, 2013

When I call attach I generally want to be able to set other attributes on the M2M relation.

A common example might be a timestamp.

Something like collection.belongsToMany(db.Models.Link).attach(link, {archived: false})

I'd also like to be able to set defaults and methods for the M2M relation.

class CollectionsLinks extends Bookshelf.Model
  defaults:
    archived: false

I found the following which seems to indicate you can't pass in a model and additional params - I'd have to create a hash myself.

      // If the item is an object, it's either a model
      // that we're looking to attach to this model, or
      // a hash of attributes to set in the relation.
      if (_.isObject(item)) {
        if (item instanceof Model) {
          data[relatedData.foreignKey] = item.id;
        } else {
          _.extend(data, item);
        }
      } else if (item) {
        data[relatedData.foreignKey] = item;
      }

Also the comments for belongsToMany say:

The joinTableName may be replaced with another object, will serve as the joining model.

I expected to be able to pass a Bookshelf.Model, whose defaults would be set on attach, as well as timestamps, etc.

Examples of using many to many relations with additional attrs on the join table would be helpful.

@vjpr
Copy link
Author

vjpr commented Jun 20, 2013

At the moment I'm just creating a join table model like so:

class Collection
  addLink: (link) =>
      cl = db.Models.CollectionLink.forge
        collectionId: @id
        linkId: link.id
        order: 0
      cl.save()

@tgriesser
Copy link
Member

So I think I intended to be able to pass a model as a joinTableName but hadn't quite gotten there yet... If you want to try to work out an implementation/tests for it as a PR, that'd be great...I think the issue may have been with the need for a composite key based on the foreignKey & otherKey but also potentially allowing an idAttribute id... I'm not sure, I'll take out that comment for now so it's not confusing.

Right now you can do this:

collection.belongsToMany(db.Models.Link).attach(link.attributes)

And the object that is set as the attributes will be saved with additional columns, but I realize that doesn't help with wanting a model/methods/timestamps on the join table... I'll think a bit more about this one.

@tgriesser
Copy link
Member

Oh... I know what I was going to do here - the equivalent of ActiveRecord's hasMany :through - where you could do

links: function() {
   return this.hasMany(Links, CollectionsLinks);
}

And then you'd have a CollectionsLinks model to work with for the joining table... Didn't quite work through the specifics, including how you'd access the join model or how the the fetch/eager loading on that would work... Does that make sense though?

@vjpr
Copy link
Author

vjpr commented Jun 20, 2013

Yep that makes sense.

@tgriesser tgriesser mentioned this issue Aug 7, 2013
8 tasks
@tgriesser
Copy link
Member

So @vjpr - this is sort of in the works. With 0.3.0 I added .belongsToMany(Model).through(OtherModel) which works well for retrieval, but I'm still working out how attaching & working with the OtherModel happens. I'll keep you posted when I make progress on this one.

@mistakia
Copy link

Hey, just wondering if there's an update on how to attach relations with a join model/table with additional attributes (timestamps, etc)?

@ErisDS
Copy link
Contributor

ErisDS commented Jun 29, 2015

I ended up here with the same question after searching 😉, I then found the following hints, which I thought it would be helpful to post here:

@rhys-vdw
Copy link
Contributor

Hi @ErisDS. Thanks for pointing this out. I'll try to update the docs soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants