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.

hasManyBy for Many To Many?

See original GitHub issue

Describe the feature

I have a backend with contact and contact emails that get serialized out like this:

a contact:

{
  "id": 1,
  "name": "Wanda",
  "emails": [
    {
      "id": 10,
      "contact_ids": [1],
      "email_address": "wanda@wanda.com"
    }
  ]
}

On the backend, these models are related many-to-many. There should be a way for pinia-orm to be able to save either a contact or contact email and have the relationships work even when the backend data coming in is an array of ids instead of a serialized object, similar to hasManyBy but works with a pivot table.

Additional information

  • Would you be willing to help implement this feature?
  • Could this feature be implemented as a module?

Final checks

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
salcedocommented, Sep 22, 2022

That’ll do it. Thank you so much.

0reactions
CodeDreddcommented, Sep 23, 2022

What about this solution?

export class Contact extends Model {
  static fields() {
    return {
      id: this.uid(),
      name: this.string(""),
      emails: this.belongsToMany(
        ContactEmail,
        ContactEmailContact,
        "contactId",
        "contactEmailId"
      ),
    };
  }
}
Contact.entity = "contacts";

export class ContactEmail extends Model {
  static fields() {
    return {
      id: this.uid(),
      email_address: this.string(""),
      contacts: this.attr([]),
      contactObjects: this.hasManyBy(Contact, 'contacts')
    };
  }
  static saving(model) {
    useRepo(ContactEmailContact).save(model.contacts.map(contactId => ({
      contactEmailId: model.id,
      contactId: contactId
    })))
  }
  static deleting(model) {
    useRepo(ContactEmailContact)
    .whereIn('contactId', model.contacts)
    .where('contactEmailId', model.id)
    .delete()
  }
}
ContactEmail.entity = "contact_emails";

class ContactEmailContact extends Model {
  static fields() {
    return {
      contactEmailId: this.uid(),
      contactId: this.uid(),
    };
  }
}
ContactEmailContact.entity = "contact_email_contact";
ContactEmailContact.primaryKey = ["contactEmailId", "contactId"];
Read more comments on GitHub >

github_iconTop Results From Across the Web

laravel - Has many through many-to-many - Stack Overflow
Show activity on this post. products and metrics have a many-to-many relationship with a pivot column of value. deals and products also have...
Read more >
Eloquent: Relationships - The PHP Framework For Web Artisans
Many-to-many relations are slightly more complicated than hasOne and hasMany relationships. An example of a many-to-many relationship is a user that has many...
Read more >
hasManyThrough with Many-To-Many? - Laravel.io
This function grabs product information for each item that is attached to an order. Many-to-Many-Through-Many. public function products() { return $this-> ...
Read more >
Laravel hasmanythrough but with manytomany - Laracasts
I have tried to use HasMany through but It doesn't work with a many to many relationship as far as I can tell....
Read more >
Laravel Has Many Through Relationship Explained with ...
These are: One To One, One To Many, One To Many (Inverse) / Belongs To, Many To Many. I like Many To Many...
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