murph-doc/docs/entities/factory.md
Simon Vieille 385af624a6
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
update theme
2023-02-09 21:50:06 +01:00

35 lines
787 B
Markdown

# Factory
Each entity should have a factory that helps to generate a new entity.
A factory must implements `App\Core\Factory\FactoryInterface`.
## Generation
A factory is basically a service which contain at lease a method named `create`.
The generation is performed in CLI. These information are required:
* The name of the futur factory (eg: `MyEntityFactory`)
* The namespace of the entity (eg: `MyEntity`)
Simply run `php bin/console make:factory`.
## Usage
```php-inline title="src/Controller/FooController.php"
namespace App\Controller;
use App\Factory\MyEntityFactory;
use Symfony\Component\HttpFoundation\Response;
class FooController
{
public function foo(MyEntityFactory $factory): Response
{
$entity = $factory->create();
// ...
}
}
```