Bookshelf: Are Many To Many Polymorphic Relations possible?

Created on 2 May 2015  路  3Comments  路  Source: bookshelf/bookshelf

I'm currently weighing up pros and cons on ORMs to use for a project. I have looked through the docs and it appears you can do polymorphic relations which is awesome, but is it possible to do many-to-many polymorphic relations?

feature relations

Most helpful comment

Hi @elliotlings @jbruni ! To deal with that I hacked belongsToMany.

  pictures: function () {
    return this
      .belongsToMany('Picture', 'picture_mapping', 'imageable_id', 'picture_id')
      .query(qb => {
        qb.where('imageable_type', 'user');
      });
  },

All 3 comments

Yes... it would be great to have many-to-many polymorphic support, just like Laravel's Eloquent:

https://laravel.com/docs/5.1/eloquent-relationships#many-to-many-polymorphic-relations

Hi @elliotlings @jbruni ! To deal with that I hacked belongsToMany.

  pictures: function () {
    return this
      .belongsToMany('Picture', 'picture_mapping', 'imageable_id', 'picture_id')
      .query(qb => {
        qb.where('imageable_type', 'user');
      });
  },

Any update on this ? :)

Was this page helpful?
0 / 5 - 0 ratings