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

Multiple subresources are created when Observer saves main resource again #126

Open
holyboly opened this issue Jan 22, 2021 · 2 comments
Open

Comments

@holyboly
Copy link

holyboly commented Jan 22, 2021

I use the plugin among other things to manage articles and, directly when creating, to be able to deposit one or more article images.

Furthermore, I use an observer for the articles, which after an article has been created under certain circumstances saves it again.

In the file vendor/yassi/nova-nested-form/src/NestedForm.php in line 342 a saved hook is used, which in my circumstances ensures that the article image is created directly several times.

After I changed the hook to created, the error no longer occurs. However, I don't know if this could cause other problems.

Here is the excerpt from the code of the corresponding place:

protected function fillAttributeFromRequest(NovaRequest $request, $requestAttribute, $model, $attribute)
{
    if ($model->exists) {
        $newRequest = NovaRequest::createFrom($request);
        $children = collect($newRequest->get($requestAttribute));
        $newRequest->route()->setParameter('resource', $this->resourceName);
        $this->deleteChildren($newRequest, $model, $children);
        $this->createOrUpdateChildren($newRequest, $model, $children, $requestAttribute, $this->getRelatedKeys($newRequest));
    } else {
        $model::saved(function ($model) use ($request, $requestAttribute, $attribute) {
            $this->fillAttributeFromRequest($request, $requestAttribute, $model, $attribute);
        });
    }
}
@yassilah
Copy link
Owner

Hmm I see what the problem is. It's not possible to use the "created" hook as this would then prevent the nested form relationships to be updated when the parent already exists (hence the use of the "saved" hook, which is called both on creation and on update). However, it might be possible to use something like this:

$eventName = 'nested-form:saved';
$dispatcher = $model::getEventDispatcher();
     
$model::registerModelEvent($eventName, function ($model) use ($request, $requestAttribute, $attribute, $eventName) {
    $this->fillAttributeFromRequest($request, $requestAttribute, $model, $attribute);
     
    if ($dispatcher) {
        $dispatcher->forget("eloquent.{$eventName}");
    }
});

$model::saved(function ($model) use ($request, $requestAttribute, $attribute, $eventName) {
    $model->fireModelEvent($eventName);
});

Could you try that out and let me know if that's what you're looking for?

@holyboly
Copy link
Author

The fireModelEvent function is protected, therefore not callable like in your example. But that seems like a possible solution if you add a custom Trait of your NestedForm Plugin to the model with a fireNestedFormEvent method which then calls the fireModelEvent method directly in the model itself?!

I tried something silly:

$model::saved(function ($model) use ($request, $requestAttribute, $attribute) {
    if (self::$alreadySaved === false) {
        $this->fillAttributeFromRequest($request, $requestAttribute, $model, $attribute);
        self::$alreadySaved = true;
    }
});

But this is also not working as expected. Before 3 images have been created, with this method its still 2, instead of the expected 1.

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

No branches or pull requests

2 participants