Comments (5)
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 user_id
is returning factory:User
instead of the actual user id?
Thanks in advance.
from testdummy.
@JeffreyWay, could you chime in here, please?
from testdummy.
For now a solution for the attributesFor
method is to just do something like this.
$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.
from testdummy.
@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:
$factory('User', [
'email' => $faker->email,
]);
$factory('Identity', [
'user_id' => 'factory:User',
'name' => $faker->name,
]);
We then run TestDummy::create('Identity')
... What happens next is the following
TL;DR
- Model is created
- Related models are created, saved, and assigned to the model
- The model is saved
Long version
- The
create('Identity')
method callspersist('Identity')
persist('Identity')
will first callbuild('Identity')
build
will first get the attributes of our factory, using$attributes = getAttributes('identity', [])
(any overrides are passed in as the second parameter). This is fetched straight from your factory definition, souser_id
=factory:User
right now...build
gets the class name of our model (since named factories is a possibility),$class = $this->getFixture($name)->name;
build
calls onEloquentModel::build($class, $attributes)
(unless you use a custom implementation of theIsPersistible
interface)build
in ourEloquentModel
will return the model with the attributes filled (by calling onEloquentModel::fill
)
- We now have an instance of our model, with
user_id
=factory:User
persist('Identity')
will now call theassignRelationships
method to actually set our relationship. This is the step in which we actually get the user created (by going throughpersist
on our related model) and retrieve its id.- Save is run on our model instance.
Hopefully, this gave some insight. Now, to the problem(s) mentioned:
The boot
method is run in the __construct()
of our Identity
. This means that, since we actually instantiate the model inside of EloquentModel
when creating the entry, we will run the boot()
method before we parse any relationships. Hence, user_id
is still factory:User
at this point, and you henceforth run into mentioned issue.
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.
from testdummy.
@phroggyy Thank you for the informative explanation.
from testdummy.
Related Issues (20)
- `$faker->optional()` is not getting called foreach transaction HOT 3
- Issue with using strings as primary id HOT 1
- Create Models from json/array
- Call to undefined method User::fill() HOT 2
- Factory::build should take times() into account HOT 2
- Create factory extending/overriding an existing factory's attributes HOT 1
- More then one row beeing adding to relational models
- factory:App\Album syntax doesn't work. it said 'Integrity Constrain Violation' message. HOT 6
- Getting faker not found error running through behat
- Problem with attribute casting
- Use factories for not Eloquent model HOT 8
- Can't use array attributes
- fakerphp/faker downgraded when installing laracasts/testdummy
- Is unique() supported? HOT 7
- Object of class Closure could not be converted to string HOT 2
- Could not locate a factory HOT 9
- unique() in user Closures
- Ability to specify relation create new or use existing HOT 8
- Faker Localization HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from testdummy.