productAccessoryBuilder = $this->loadService(ProductAccessoryBuilder::class); $this->productAccessoryRepository = $this->loadService(ProductAccessoryRepository::class); } public function createProductAccessory(Product $product, Accessory $accessory, int $quantity): ProductAccessory { $productAccessory = $this->productAccessoryBuilder->instanciateProductAccessory(); $productAccessory->setProduct($product); $productAccessory->setAccessory($accessory); $productAccessory->setQuantity($quantity); $productAccessory->save(); return $productAccessory; } public function createProductAccessoryIfNotExist(Product $product, Accessory $accessory, int $quantity): ProductAccessory { $productAccessory = $this->productAccessoryRepository->findOneProductAccessory($product, $accessory); if($productAccessory) { $productAccessory->setQuantity($quantity); $productAccessory->save(); } else { $productAccessory = $this->createProductAccessory($product, $accessory, $quantity); } return $productAccessory; } public function deleteAllProductAccessoriesByAccessory(Accessory $accessory): void { $productAccessoriesArray = $this->productAccessoryRepository->findProductAccessoriesByAccessory($accessory); foreach($productAccessoriesArray as $productAccessory) { $productAccessory->delete(); } } }