public function createAction() { $em = $this->get('doctrine')->getEntityManager(); // Let's create a new thing $thing = new Thing(); $thing->setName('Lorem' . \uniqid()); // This does not actually save the entity but rather mark it // as "should-be-saved-on-the-next-flush" $em->persist($thing); // ... and let's create a new category! // Yes sir, we are in a good mood today. $category = new Category(); $category->setName('Category' . \uniqid()); $thing->setCategory($category); $em->persist($category); // Now we save everything: let's flush! $em->flush(); return $this->render('KnpIpsumBundle:DoctrineOrm:create.html.twig', array('thing' => $thing)); }
public function load(ObjectManager $em) { $cat = new Thing(); $cat->setName('Edgar the Cat'); $bull = new Thing(); $bull->setName('Joe the bull'); $categoryRed = new Category(); $categoryRed->setName('Red'); $cat->setCategory($categoryRed); $bull->setCategory($categoryRed); for ($i = 0; $i < 20; $i++) { $thing = new Thing(); $thing->setName('Dummy thing ' . $i); $thing->setCategory($categoryRed); $em->persist($thing); } $em->persist($cat); $em->persist($bull); $em->persist($categoryRed); $em->flush(); }