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

Synced model with cast on creation not work #1273

Open
galliroberto opened this issue Dec 15, 2024 · 5 comments
Open

Synced model with cast on creation not work #1273

galliroberto opened this issue Dec 15, 2024 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@galliroberto
Copy link

galliroberto commented Dec 15, 2024

Bug description

Creation of synced model not work properly.
The cast field not works by design using getAttributes.
Use unsetRelations()->attributesToArray()

UpdateSyncedResource.php

        // We disable events for this call, to avoid triggering this event & listener again.
        $event->model->getCentralModelName()::withoutEvents(function () use (&$centralModel, $syncedAttributes, $event) {
            if ($centralModel) {
                $centralModel->update($syncedAttributes);
                event(new SyncedResourceChangedInForeignDatabase($event->model, null));
            } else {
                // If the resource doesn't exist at all in the central DB,we create
                // the record with all attributes, not just the synced ones.
                $centralModel = $event->model->getCentralModelName()::create($event->model->attributesToArray());
                event(new SyncedResourceChangedInForeignDatabase($event->model, null));
            }
        });

Before
image

After
image

Steps to reproduce

try to create a synced model with casted field like array

Expected behavior

During creation of synced mode, the casted field has to keep casted

Laravel version

11.35.1

stancl/tenancy version

3.8.5

@galliroberto galliroberto added the bug Something isn't working label Dec 15, 2024
@stancl
Copy link
Member

stancl commented Dec 15, 2024

Please include more clear examples for reproducing the behavior.

@galliroberto
Copy link
Author

galliroberto commented Dec 15, 2024

Laravel getAttributes doesn't cast the attribute, so it can't create a model with the right field.

My workflow is:
Inside a tenant I create a User.
Mu user has many casted fields, two of them are arrays.

In tenant the user has the right casted field, because laravel create it.

User is synced with CentralUser.

When I create a User, your package create a CentralUser and viceversa.

Using getAttributes, retrieve the field as text and not cast it to array.
When your package create CentralUser, use not casted field, in this case is a string of json and not array as the cast expected.

Let me know if I was clear now.

However, is the behaviour of getAttributes that make it happens.

@galliroberto
Copy link
Author

galliroberto commented Dec 23, 2024

To no lose hidden attribute

$event->model->makeVisible($event->model->getHidden()); // <------------------
$centralModel = $event->model->getCentralModelName()::create($event->model->attributesToArray());
event(new SyncedResourceChangedInForeignDatabase($event->model, null));

@stancl
Copy link
Member

stancl commented Dec 23, 2024

It's likely this was fixed in v4. I'll confirm that later.

We generally won't change resource syncing behavior in v3 even for small bugs since most fixes require significant breaking changes.

@galliroberto
Copy link
Author

galliroberto commented Dec 23, 2024

I have override the method.
I write also for others want to fix it.

For me was a big bug because broke my login.

When v4 will be released?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants