public function load(ObjectManager $manager) { $ingredientNames = ['Leek', 'Carrot', 'Potato', 'Green Lentils', 'Celeriac']; foreach ($ingredientNames as $ingredientName) { $ingredient = new Ingredient(); $ingredient->setName($ingredientName); $ingredient->setDescription('This is yummi'); $manager->persist($ingredient); $this->addReference('ingredient-' . $this->toReferenceString($ingredientName), $ingredient); } $manager->flush(); }
public function postPersist(Recipe $recipe, LifecycleEventArgs $event) { if (count($recipe->getIngredients()) < 1) { return; } $entityManager = $event->getEntityManager(); $ingredient = new Ingredient(); $ingredient->setName('Salt'); $ingredient->setDescription('Salt'); $entityManager->persist($ingredient); $recipe->setCreatedAt(new \DateTime()); $recipe->addIngredient($ingredient); $entityManager->persist($recipe); $entityManager->flush(); }