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

Callbacks don't stack #472

Open
jesse-r-s-hines opened this issue Jan 5, 2021 · 0 comments
Open

Callbacks don't stack #472

jesse-r-s-hines opened this issue Jan 5, 2021 · 0 comments

Comments

@jesse-r-s-hines
Copy link

Callbacks set on the "base case" definition will be overridden by callbacks added to any groups on the same model, meaning that the first callback won't be called. Since groups definitions build on the "base case" I would expect the callbacks to stack similarly.

For example:

$fm->define("User")->setDefinitions([
    // definitions ...
])->setCallback(function ($object, $saved) {
    $object->email = "user@example.com";
});

$fm->define("mygroup:User")->setDefinitions([
    // more definitions ...
])->setCallback(function ($object, $saved) {
    $object->address = "some address";
});

Then if I create a plain User, fields email will be set and address will be blank, as expected...

$user = $fm->create("User");
$user->email; // "user@example.com"
$user->address; // null

However if I use the mygroup:User factory the first callback isn't called, only the second

$user = $fm->create("User");
$user->email; // null
$user->address; // "some address"

This requires duplicating any logic that was in the User callback in all groups.

It would be useful to have a way to "stack" the callbacks, either by modifying setCallback() to add a callback to a list instead of replacing any previous callbacks, or making an new addCallback() method that does that.

jesse-r-s-hines added a commit to jesse-r-s-hines/factory-muffin that referenced this issue Jan 5, 2021
setCallback() replaces previous callbacks making you have to repeat any
logic in the "base case' definition callback if you have a callback on
one of the groups.

See issue thephpleague#472
jesse-r-s-hines added a commit to jesse-r-s-hines/factory-muffin that referenced this issue Jan 5, 2021
setCallback() replaces previous callbacks making you have to repeat any
logic in the "base case' definition callback if you have a callback on
one of the groups.

See issue thephpleague#472
jesse-r-s-hines added a commit to jesse-r-s-hines/factory-muffin that referenced this issue Jan 5, 2021
setCallback() replaces previous callbacks making you have to repeat any
logic in the "base case' definition callback if you have a callback on
one of the groups.

See issue thephpleague#472
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

1 participant