question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Save model with hasMany

See original GitHub issue

Imagine a typical relationship between a User model and a Course model. A user can have many courses and a course can have many users. This is a normal many to many relationship. However, I also need some columns on the join table. Imagine that a discount associated with the user <-> course association.

I’m writing a web api, and I have an endpoint that accepts something like this JSON (POST /users/123):

{
  "firstName": "Miguel",
  "lastName": "Andrade",
  "courses": [
    {
      "course_id": 1,
      "discount": 0.23
    },
    {
      "course_id": 4,
      "discount": 0.33
    }
  ]
}

This is what the web app sends after the user submits a form where he/she can choose courses and their respective discounts.

The question is, what is the appropriate way to save this JSON in Bookshelf? I don’t mind transforming this many-to-many in two one-to-many. I could create a Model called CourseSubscription which could hold the relationships and the additional discount. Either way, I’d like to know how to best tackle this problem.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
rhys-vdwcommented, Dec 22, 2015

@miguelcobain See #831. Something like this, perhaps:

// CURRENTLY NOT SUPPORTED
Person.forge({
  name: 'miguel',
  friends: [{ name: 'rhys' }],
  skills: [{ skill: 'JavaScript' }, { skill: 'css' }]
}).save({ withRelated: ['friends', 'skills'] }).then(person =>

Could take withRelated: true to accept all. There’s a few things that need to happen before then though.

0reactions
miguelcobaincommented, Jan 11, 2016

@rhys-vdw Something came up that I think is somewhat related with this issue. So I thought to include it here for anyone that may need it.

The problem is related with orphans. Example:

  • I save an object like:
{
  "firstName": "Miguel",
  "lastName": "Andrade",
  "courses": [
    {
      "course_id": 1,
      "discount": 0.23
    },
    {
      "course_id": 4,
      "discount": 0.33
    }
  ]
}

Both inner objects got ids 1 and 2 respectively.

  • Then, the user removes one of the inner models and clicks save, so I’m now saving:
{
  "firstName": "Miguel",
  "lastName": "Andrade",
  "courses": [
    {
      "id": 1,
      "course_id": 1,
      "discount": 0.23
    }
  ]
}

This should issues an update, but it is intended to also generate a DELETE for the now orphan courses id 2. This is a similar feature as JPA orphanRemoval (more info).

How could I get a similar behavior? My first instinct is to use underlying knex to DELETE courses NOT IN the list of ids I’m saving. A code example would help a lot.

Also, I can create a separate issue for orphanRemoval, if you feel that it is within the scope of this project, and not just a help request. 😃

Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Laravel 'save()' for 'hasmany' Model - Stack Overflow
Show activity on this post. If I have a two models, with a hasMany relationship, so my Car hasmany CarOwners , I would...
Read more >
How to store and update hasMany relationship - Laracasts
Hello, i make relationship between User Model and Exam Model but i can't update or store this ... or new up an exam...
Read more >
Eloquent: Relationships - The PHP Framework For Web Artisans
The save Method; The create Method; Belongs To Relationships; Many To Many Relationships ... The hasOne method is available to your model via...
Read more >
Working with Relationships - Models - Ember Guides
save ();. It is also possible to remove a record from a hasMany relationship:
Read more >
Associating models without saving them directly - Laravel.io
I've update my post. I know the associate function works for the "belongs to" relationship, but is there something similar for a "has...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found