/**
  * {@inheritDoc}
  */
 public function load(ObjectManager $manager)
 {
     $data = $this->userFixtureDataSource();
     // Have to keep a reference to all the user objects
     // or else they can't be flushed all at once.
     $items = array();
     $user = new User();
     foreach ($data['User'] as $item) {
         $user = new User();
         $user->setUuid($item['uuid'])->setUsername($item['username'])->setPassword($item['password'])->setEmail($item['email'])->setIsActive($item['is_active']);
         $this->addReference($user->getUuid(), $user);
         $items[] = $user;
         $manager->persist($user);
     }
     $manager->flush();
     $items = array();
     foreach ($data['Record'] as $item) {
         $record = new Record();
         $record->setCategory($item['category'])->setKey($item['aKey'])->setData($item['data'])->setOwner($manager->merge($this->getReference($item['owner_uuid'])));
         $timestamp = new \DateTime('now');
         if (!empty($item['timestamp'])) {
             $timestamp->setTimestamp($item['timestamp']);
         }
         $record->setTimestamp($timestamp);
         $items[] = $record;
         $manager->persist($record);
     }
     $manager->flush();
 }
 /**
  * @Route("/{uuid}/{category}/{key}", requirements={"uuid" = "[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}"})
  * @Method({"POST"})
  */
 public function postAction(Request $request, $uuid, $category, $key)
 {
     $response = new TextPlainResponse('Huh?', 503);
     $doctrine = $this->getDoctrine();
     $em = $doctrine->getManager();
     $conn = $em->getConnection();
     $recordRepository = $doctrine->getRepository('VirtualPersistBundle:Record');
     $user = $doctrine->getRepository('VirtualPersistBundle:User')->findOneByUuid($uuid);
     if ($user) {
         $record = $recordRepository->findOneByUserCategoryKey($user, $category, $key);
         if ($record) {
             try {
                 $conn->beginTransaction();
                 $record->setData($request->request->get('data', ''));
                 $record->setTimestamp(new \DateTime('now'));
                 $em->persist($record);
                 $em->flush();
                 $conn->commit();
             } catch (\Exception $e) {
                 $conn->rollback();
                 $em->close();
                 throw $e;
             }
         } else {
             // no record
             // Glean the data to post.
             $data = $request->request->get('data', '');
             $record = new Record();
             $record->setOwner($user);
             $record->setCategory($category);
             $record->setKey($key);
             $record->setData($data);
             $record->setTimestamp(new \DateTime('now'));
             // Isolate writing the new record in a transaction.
             $conn->beginTransaction();
             try {
                 $em->persist($record);
                 $em->flush();
                 $conn->commit();
             } catch (\Exception $e) {
                 $conn->rollback();
                 $em->close();
                 throw $e;
             }
         }
         $response = new TextPlainResponse('Item added.', 200);
     } else {
         // no such user
         $response = new TextPlainResponse('No such item.', 404);
     }
     return $this->addDebugInfo($response);
 }