How to deal with deeply nested and interconnected relationships in Factories?
I'm building a pretty large application that has several interconnected models. Here are a few examples:
Teams
This is the Jetstream Teams model. Almost all other models belong to this model.
Orders
Customer Orders. Belongs to a customer and a team.
Customers
Belongs to a team
Has many orders
OrderItems
Belongs to an order.
Belongs to a product.
Products
Belongs to a team
So, I need to create an Order, that has many order items, and each order item product must be related to the same team as the order. Same with the customer relationship.
How would you set up a factory to seed this kind of relationship to ensure they all belong to the same team?
You can create factories with relationships, so choose the base model, make a factory for that, and then in the has, pass in the relationships it requires, and then, pass in the relationships that related model requires.
For the next person, there is a built in solution for this:
If you have models that share a common relationship with another model, you may use the recycle method to ensure a single instance of the related model is recycled for all of the relationships created by the factory.