|
| 1 | +# Model Factory |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +Factory classes are used to programmatically create instances of models. This is useful for seeding databases, creating test data, and other situations where you need to create a lot of model instances. |
| 6 | + |
| 7 | +```php |
| 8 | +<?php |
| 9 | + |
| 10 | +namespace Give\Campaigns\Factories; |
| 11 | + |
| 12 | +class CampaignFactory extends Give\Framework\Models\Factories\ModelFactory |
| 13 | +{ |
| 14 | + public function definition(): array |
| 15 | + { |
| 16 | + return [ |
| 17 | + 'title' => __('GiveWP Campaign', 'give'), |
| 18 | + 'description' => $this->faker->paragraph(), |
| 19 | + ]; |
| 20 | + } |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +Each instance of a model created by a factory class is populated with default values defined in the `definition` method, which can be hard-coded or generated dynamically using integrated the `fakerphp/faker` library. |
| 25 | + |
| 26 | +## Creating Models with Factories |
| 27 | + |
| 28 | +A model can be instantiated using the factory `make()` method, which uses the defaults provided by the `definition()` method to create the model instance. |
| 29 | + |
| 30 | +```php |
| 31 | +use Give\Campaigns\Models\Campaign; |
| 32 | + |
| 33 | +$campaign = Campaign::factory()->make(); |
| 34 | +``` |
| 35 | + |
| 36 | +Additionally, multiple model instances can be created using the `count()` method. |
| 37 | + |
| 38 | +```php |
| 39 | +use Give\Campaigns\Models\Campaign; |
| 40 | + |
| 41 | +$campaigns = Campaign::factory()->count(3)->make(); |
| 42 | +``` |
| 43 | + |
| 44 | +### Overriding Attributes |
| 45 | + |
| 46 | +You can override these default values by passing an array of attributes to the factory's `make()` or `create()` method. |
| 47 | + |
| 48 | +```php |
| 49 | +use Give\Campaigns\Models\Campaign; |
| 50 | + |
| 51 | +$campaign Campaign::factory()->create([ |
| 52 | + 'title' => 'My Custom Campaign', |
| 53 | +]); |
| 54 | +``` |
| 55 | + |
| 56 | +### Persisting Models |
| 57 | + |
| 58 | +The `create()` method instantiates model instances and persists them to the database using model's `save()` method. |
| 59 | + |
| 60 | +```php |
| 61 | +use Give\Campaigns\Models\Campaign; |
| 62 | + |
| 63 | +$campaign Campaign::factory()->create(); |
| 64 | +``` |
| 65 | + |
| 66 | +### Deferred Resolution |
| 67 | + |
| 68 | +Sometimes you may need to defer the resolution of an attribute until the model is being created. Either the attribute definition is not a simple value or is otherwise expensive to instantiate. |
| 69 | + |
| 70 | +Factory attribute definitions can be deferred using `Closure` callbacks instead of a hard-coded or generated value. |
| 71 | + |
| 72 | +```php |
| 73 | + public function definition(): array |
| 74 | + { |
| 75 | + return [ |
| 76 | + 'title' => __('GiveWP Campaign', 'give'), |
| 77 | + 'description' => function() { |
| 78 | + return prompt('Write a short description for a fundraising campaign.') |
| 79 | + }, |
| 80 | + ]; |
| 81 | + } |
| 82 | +``` |
| 83 | + |
| 84 | +Additionally, deferred attributes are not resolved when the attribute is overridden, which prevents unnecessary computation when the default value is not used. |
| 85 | + |
| 86 | +```php |
| 87 | +$campaign = Campaign::factory()->create([ |
| 88 | + 'description' => 'My custom description', |
| 89 | +]); |
| 90 | +``` |
| 91 | + |
| 92 | +## Model Relationships with Factories |
| 93 | + |
| 94 | +Attribute definitions can also be other model factories, which can be resolved to a property value, such as an ID, to create required dependencies. |
| 95 | + |
| 96 | +```php |
| 97 | + public function definition(): array |
| 98 | + { |
| 99 | + return [ |
| 100 | + 'title' => __('GiveWP Campaign', 'give'), |
| 101 | + 'formId' => DonationForm::factory()->createAndResolveTo('id'), |
| 102 | + ]; |
| 103 | + } |
| 104 | +``` |
| 105 | + |
| 106 | +This is particularly useful when defining model relationships, where the related model is only instantiated when a model is not explicity provided as an override. |
| 107 | + |
| 108 | +If an existing model (or model ID) is provided as an override, the factory will not instantiate an additional model. |
| 109 | + |
| 110 | +```php |
| 111 | +Campaign::factory()->create([ |
| 112 | + 'formId' => $donationForm->id, |
| 113 | +]); |
| 114 | +``` |
0 commit comments