-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[11.x] add ability to disable relationships in factories #53450
[11.x] add ability to disable relationships in factories #53450
Conversation
sometimes when `make`ing or `create`ing a model, we don't actually care about the relationship. this new property and method allow us to turn them all off at once. when disabled, any attribute that is assigned a factory will instead return `null`.
Would the state of this property be lost if you called a method like |
that name change seems fine to me. do you want the property adjusted as well to I'm not positive, but you're probably right that anything that builds a new object probably would lose this state. the whole factory is a little bit of a mess of static vs object. not sure how you would want to handle this. I believe I had a PR a long time ago that got rid of the whole static new object paradigm but it was closed. |
@taylorotwell I changed the method name to your suggestion, and also updated the property name, and inverted the logic so it matched semantically. I also persisted the state (I think) across calls to this is a separate discussion, but I'd love to talk about what the value of making the factories immutable was/is. it makes sense for Collections, but I'm really just struggling to see the value in factories, and it's making new features and maintenance difficult IMO. |
Hey @browner12 - sorry about the immutable stuff... was it really that bad? It looks like you just had to pass the property through the constructor? 😅 |
@taylorotwell it's definitely doable. what I'm struggling with though is I don't see the value of immutability on the factories. maybe I don't fully understand how everyone uses them, so I'd like to gain perspective on that. but if not, I think there'd be value in refactoring the immutability out, and simplifying the factories greatly. |
@browner12 was this released? I thought so but can't find it anymore. |
Ah I see it was renamed later to: withoutParents 👍 |
@christophrumpel was it really? lol |
sometimes when
make
ing orcreate
ing a model, we don't actually care about the relationships. this new property and method allow us to turn them all off at once. when disabled, any attribute that is assigned a factory will instead returnnull
.imagine an
Organization
andUser
models. theorganizations
table is:and the
users
table is:When we define the
UserFactory
we setup the relationship with a factory:This is great for when we seed and when we run feature tests, because it will automatically create the
Organization
s for us.However, this is not always desired, often when you're writing Unit tests. Imagine my user model has an
isAdult()
function:To write a Unit test for this I would
make
a User:Unfortunately, this would try storing Organizations in a database, even though we don't care about that. It would also fail if you didn't have
RefreshDatabase
or something similar on your test.With this PR, you could easily disable all relationships defined in your factories and make this test work: