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

[9.x] Fix evaluation of factories returned from definition closure #46891

Closed
wants to merge 1 commit into from

Conversation

ctalkington
Copy link

@ctalkington ctalkington commented Apr 26, 2023

this was originally fixed in #42344 but in #44107 this was changed to support recycling which removed the getKey call so if you had a closure returning a factory in the definition in your factory it would cause an sql error with trying to insert the string representation of the resolved model (in second map of this function) into the database because the factory ends up resolved to model instead of the key. This appears to only happen when using a recycled model via recycle in my testing which is when getRandomRecycledModel returns model.

note: #44328 changed the method to support multiple models but remained without getKey call.

class PostFactory extends Factory
{
    public function definition()
    {
        return [
            'title' => 'post',
            'user_id' => function (array $attributes) {
                return User::factory();
            },
        ];
    }
}

@nunomaduro
Copy link
Member

Could you please add tests to ensure that this new code does not cause any issues with existing applications and functions as intended?

@nunomaduro nunomaduro self-assigned this May 13, 2023
@nunomaduro
Copy link
Member

Hi @ctalkington, once you have this code/pull request adjusted with tests, please create a new pull request. Thank you!

@nunomaduro nunomaduro closed this May 15, 2023
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

Successfully merging this pull request may close these issues.

2 participants