Skip to content

How to sync multiple relations? #95

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

Open
Stiropor opened this issue Jan 2, 2024 · 5 comments
Open

How to sync multiple relations? #95

Stiropor opened this issue Jan 2, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@Stiropor
Copy link

Stiropor commented Jan 2, 2024

Description

First of all - great package! Works really nice.

I do have a question - mutation endpoint supports also sync, but somehow I cannot figure out how to make sync for multiple related resources. For example an article has a BelongsToMany relation with tags. When tags change on an article - how do I send only new ids (keys) of tags to sync, as I would do with ->sync() method in Laravel?

This is what I'm using for now:

{
  "mutate": [
    {
        "operation": "update",
        "key": 42,
        "attributes": {
            "title": "Test"
        },
        "relations": {
            "tags": [
                {
                    "operation": "sync",
                    "without_detaching": true,
                    "key": 2
                },
                {
                    "operation": "sync",
                    "without_detaching": true,
                    "key": 3
                }
            ]
        }
    }
  ]
}

If I don't send without_detaching only last tag is then attached to articles. If I add without_detaching then new ones are added but old ones (id: 1) are still there.

It'd be maybe cool to either use keys (array) attribute, to sync multiple related models or just making key attribute also being allowed as an array, but this could of course make things more complex I suppose.

Any ideas?

@GautierDele
Copy link
Member

Hello @Stiropor,

I understand your problem, this might be a good option to be able to provide multiple keys as you are giving me the point, I'll put this as a new feature 😄

While you wait for this to be done, what you can simply do is remove the "without_detaching" from the first argument, this will detach all former ids do you see what I mean ?

@GautierDele GautierDele added the enhancement New feature or request label Jan 4, 2024
@Stiropor
Copy link
Author

While you wait for this to be done, what you can simply do is remove the "without_detaching" from the first argument, this will detach all former ids do you see what I mean ?

This works, yes. The problem with this is when all tags are deleted, I cannot send empty [] and ask for sync. I'd always need to know which tags there were before.

@GautierDele
Copy link
Member

I'm not sure I understand your problem, could you please precise it ?

@Stiropor
Copy link
Author

Stiropor commented Jan 28, 2024

I'm not sure I understand your problem, could you please precise it ?

Certainly. I'm using this to handle belongsToMany relation, in this case article with tags. When adding tags, there is no problem. When modifying them I can also use sync (without without_detaching on first one it works). But when editing an article and all tags are removed there is nothing to send to use sync as I'd normally use it in Laravel to sync relations. The only way would be to somehow remember original attributes of article, which is kinda unusual and not useful otherwise. Doing a sync with [] would be great in this case or sending empty key, which of course is not the current feature. Are there any other way of removing all relations (tags for example) from a spcific entity (article for example)?

@Oersta1rdt
Copy link

This is lovely but I'm yet to understand what you are doing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants