-
Notifications
You must be signed in to change notification settings - Fork 80
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
Relationship attributes not being added to model instance before saving? #95
Comments
I am getting a similar error. $factory('User', [
'email' => $faker->email,
]);
$factory('Profile', [
'user_id' => 'factory:User',
'name' => $faker->name,
]); $profile = TestDummy::attributesFor('Profile', $overrides); This will return something like this [
'user_id' => "factory:User",
'name' => "Foo"
] Is there a reason why the Thanks in advance. |
@JeffreyWay, could you chime in here, please? |
For now a solution for the $user = TestDummy::create('User');
$profile = TestDummy::attributesFor('Profile', ['user_id' => $user->id]); It would be cool to have this done automatically but this works for now. |
@borfast @nisaac2fly: Having just submitted a PR for TD, I spent quite some time with the source code last night, so I'll try to go through how TD works behind the scenes when different commands are called. Let's suppose we have the following setup:
We then run TL;DR
Long version
Hopefully, this gave some insight. Now, to the problem(s) mentioned: The I might try to work on a way to create related models first, but it is really quite difficult since that means changing the order of operations on TestDummy and not creating the instance before assigning relationships. |
@phroggyy Thank you for the informative explanation. |
My factory.php has this:
My Identity model has this:
Finally, in one of my tests there's this:
Executing that test results in an exception in the Identity code, specifically in the
$email = $identity->user->email
line:ErrorException: Trying to get property of non-object
Turns out $identity->user is null, even though it is specified in the factory definition.
If I comment out the boot() method in my Identity model, I get an SQL integrity constraint violation:
Integrity constraint violation: 1048 Column 'user_id' cannot be null
In the end there is a User record in my database but no Identity. My first thought was that relationship attributes are not being added to the Identity model instance before trying to save it, which could cause both problems, but if that's the case, how would the library be able to automatically and recursively create the related objects?
Am I missing something?
The text was updated successfully, but these errors were encountered: