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

fixed bug when function wrapped around definition of related factory #47115

Closed
wants to merge 1 commit into from

Conversation

Ken-vdE
Copy link
Contributor

@Ken-vdE Ken-vdE commented May 17, 2023

This fixes a bug which arises when a factory instance is wrapped by a closure within a factory definition, combined with recyclable models.
Then a recyclable model gets retrieved, but is never converted to its key.
This causes an error because the INSERT statement tries to insert the entire model as json instead of simply its key, e.g.:

SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '{"id":2,"external_id":"1","name":"Abel","email":"[email protected]","role":"super-admin","status":"offline","status_confirmed_at":' for column 'user_id' at row 1 (SQL: insert into `text_messages` (`text_conversation_id`, `user_id`, `direction`, `subject`, `body`, `service_data`, `updated_at`, `created_at`) values (50, {"id":2,"external_id":"1","name":"Abel","email":"[email protected]","role":"super-admin","status":"offline","status_confirmed_at":null,"conversation_info":null,"administration_id":1,"google_refresh_token":null,"worker_sid":null,"notification_preferences":null,"home_screen_settings":null,"created_at":"2023-05-17T12:52:25.000000Z","updated_at":"2023-05-17T12:52:25.000000Z","deleted_at":null,"last_name":"Jacobs","full_name":"Abel Jacobs","pivot":{"company_id":1,"user_id":2}}, OUT, Labore excepturi et quod provident nulla., <html><head><title>In ipsam eos saepe.</title></head><body><form action="example.org" method="POST"><label for="username">aliquam</label><input type="text" id="username"><label for="password">harum</label><input type="password" id="password"></form><div id="37470"><div id="77081"></div><div id="52460"></div></div></body></html>
, [], 2023-05-17 14:52:26, 2023-05-17 14:52:26))

@taylorotwell
Copy link
Member

No example given.

@Ken-vdE
Copy link
Contributor Author

Ken-vdE commented May 17, 2023

@taylorotwell Here's an example:

Let's say we have this factory:

class TextConversationFactory extends Factory
{
    protected $model = TextConversation::class;

    public function definition(): array
    {
        return [
            'company_id' => Company::factory(),
            'webshop_id' => fn($a) => Webshop::factory(\Arr::only($a, 'company_id')),
            'subject' => $this->faker->sentence(),
        ];
    }
}

and we have this seeder:

class TextConversationSeeder extends Seeder
{
    public function run(): void
    {
        $companies = Company::with(['webshops'])->get();
        foreach ($companies as $company) {
            TextConversation::factory(mt_rand(16, 32))
            ->for($company)
            ->recycle($company->webshops)
            ->create();
        }
    }
}

The seeder would throw an error as stated in the description because the recycled Webshop model would not be parsed to its key within the factory.

@Ken-vdE
Copy link
Contributor Author

Ken-vdE commented May 19, 2023

Hi @taylorotwell,
have you seen my previous message? It includes the example you requested.

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