murph-doc/docs/entities/factory.md

35 lines
787 B
Markdown
Raw Normal View History

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