Closed GrahamCampbell closed 10 years ago
I like :)
Maybe custom getter too while we are at it?
Where do we get the attributes?
Ignore me, I just at a large popcorn at the cinima, I am probably drunk on sugar.
Lol, no problem. I'll write up some docs and tests for this then. :)
@scottrobertson While you're here, what do you think about #287?
Just replied - I like it.
@scottrobertson Final call before I merge this.
I'll leave this till tomorrow to give you time to review it.
I'm not sure if I understand this fully but is the custom maker global?
I'm looking for a way to define a custom maker for certain classes.
I previously added #34 because for some of our models our criteria for setting their attributes is a bit complex, ie some fields can only be entered when others exist.
This means that our factory methods sometimes randomly generate an attribute, and then depending on that attribute might generate more data.
I think it would be useful to be able to say
League\FactoryMuffin\Facade::defineFactory('MyModel', function ($model, $faker) {
// Fill in model attributes with faker
return $model;
})
Or maybe even this as a short hand...
League\FactoryMuffin\Facade::defineFactory('MyModel', array $attrs = [], function ($model, $faker) {
// $attrs already set as normal on model, but this closure can customize some additional attrs
return $model;
})
We are still using the old package for now but this is something we'd need to be able to upgrade.
I thought I would ask for some feedback before writing a pull request though.
@spencerdeinum that callback already exists on the ::define method, so you can already do what you are asking. (See #277). We are working on making faker available inside that callback.
Ah, I think Github was defaulting to the 2.0 release.
That is great, I really like the new version of this package guys, thanks for all your hard work :)
@spencerdeinum it is defaulting to 2.0 yeah :)
and thank you very much. More to come hopefully.
Feel free to checkout our upgrading guide to our 2.1 dev version over on the master branch. We make sure the docs are always kept up to date. There are no BC breaks anyway - the guide mainly introduces new things and highlights a few querks.
You can install it by requiring:
{
"require-dev": {
"league/factory-muffin": "2.1.*@dev"
}
}
@GrahamCampbell, just a detail, what do you think of using call_user_func($setter)
instead $setter(...)
?
That way, I can pass an closure, an array with object/method or a function name.
If you really need it, I can change it. I just chose the other way for performance reasons. You'd notice a slow down if you were seeding 1,000 times for example.
Ok, I do not really need it. And if I did, I could call my functions inside a closure...
This is something to consider for 3.0. Maybe we could change the "closure" generator to a "callable" generator too.
This is just a proof of concept for #285. Provided that this solves @dimrsilva's issue, and @scottrobertson is ok with this, I'll add tests and update the documentation.