|
| 1 | +# Object registry |
| 2 | + |
| 3 | +Imagine that in an `ItemJob` you need to find objects from a database. |
| 4 | + |
| 5 | +```php |
| 6 | +use App\Entity\Product; |
| 7 | +use Doctrine\Persistence\ObjectRepository; |
| 8 | +use Yokai\Batch\Job\Item\ItemProcessorInterface; |
| 9 | + |
| 10 | +class DenormalizeProductProcessor implements ItemProcessorInterface |
| 11 | +{ |
| 12 | + public function __construct( |
| 13 | + private ObjectRepository $repository, |
| 14 | + ) { |
| 15 | + } |
| 16 | + |
| 17 | + /** |
| 18 | + * @param array<string, mixed> $item |
| 19 | + */ |
| 20 | + public function process(mixed $item): Product |
| 21 | + { |
| 22 | + $product = $this->repository->findOneBy(['sku' => $item['sku']]); |
| 23 | + |
| 24 | + $product ??= new Product($item['sku']); |
| 25 | + |
| 26 | + $product->setName($item['name']); |
| 27 | + $product->setPrice($item['price']); |
| 28 | + //... |
| 29 | + |
| 30 | + return $product; |
| 31 | + } |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +The problem here is that every time you will call `findOneBy`, you will have to query the database. |
| 36 | +The object might already be in Doctrine's memory, so it won't be hydrated twice, but the query will be done every time. |
| 37 | + |
| 38 | +The role of the `ObjectRegistry` is to remember found objects identities, and query these objects with it instead. |
| 39 | + |
| 40 | +```diff |
| 41 | +use App\Entity\Product; |
| 42 | +-use Doctrine\Persistence\ObjectRepository; |
| 43 | ++use Yokai\Batch\Bridge\Doctrine\Persistence\ObjectRegistry; |
| 44 | +use Yokai\Batch\Job\Item\ItemProcessorInterface; |
| 45 | + |
| 46 | +class DenormalizeProductProcessor implements ItemProcessorInterface |
| 47 | +{ |
| 48 | + public function __construct( |
| 49 | +- private ObjectRepository $repository, |
| 50 | ++ private ObjectRegistry $registry, |
| 51 | + ) { |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @param array<string, mixed> $item |
| 56 | + */ |
| 57 | + public function process(mixed $item): Product |
| 58 | + { |
| 59 | +- $product = $this->repository->findOneBy(['sku' => $item['sku']]); |
| 60 | ++ $product = $this->registry->findOneBy(Product::class, ['sku' => $item['sku']]); |
| 61 | + |
| 62 | + $product ??= new Product($item['sku']); |
| 63 | + |
| 64 | + $product->setName($item['name']); |
| 65 | + $product->setPrice($item['price']); |
| 66 | + //... |
| 67 | + |
| 68 | + return $product; |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +The first time, the query will hit the database, and the object identity will be remembered in the registry. |
| 74 | +Everytime after that, the registry will call `Doctrine\Persistence\ObjectManager::find` instead. |
| 75 | +If the object is still in Doctrine's memory, it will be returned directly. |
| 76 | +Otherwise, the query will be the fastest possible because it will use the object identity. |
| 77 | + |
| 78 | + |
| 79 | +## On the same subject |
| 80 | + |
| 81 | +- [What is an item job ?](https://github.com/yokai-php/batch/blob/0.x/docs/domain/item-job.md) |
0 commit comments