コード例 #1
0
 /**
  * @Security("has_role('ROLE_APP_ADMIN')")
  */
 public function createParamAction($name, $price)
 {
     $category = $this->initCategory();
     $product = new Product();
     $product->setName($name);
     $product->setPrice($price);
     $product->setDescription("NULL - " . $name);
     $product->setCategory($category);
     $validator = $this->get("validator");
     $errors = $validator->validate($product);
     if (count($errors) > 0) {
         $errorsString = (string) $errors;
         return $this->render("DWSBundle::index.html.twig", array("text" => $errorsString));
     }
     $this->addAction($product);
     return new Response("Created product:\n\t\t\t\t\t\t\t\t\n Id: " . $product->getId() . "\n Category: " . $product->getCategory()->getName() . "\n\tName: " . $product->getName() . "\n\tPrice: " . $product->getPrice() . "\n Description: " . $product->getDescription() . "\n\n");
 }
コード例 #2
0
 public function load(ObjectManager $manager)
 {
     $root = $this->container->getParameter('kernel.root_dir');
     $dir = str_replace('/app', '/src/DWSBundle/Resources/data/', $root);
     $fd = fopen($dir . 'products.csv', "r");
     //$repository = $this->getDoctrine()
     //    ->getRepository("DWSBundle:Category");
     //$categories = $repository->findAll();
     //$service = new FixturesService();
     //$categories = $service->runCategories();
     $service = $this->container->get('dws.category_service');
     $categories = $service->getCategories();
     //$controller = $this->get('dws.category_controller');
     //$categories = $controller->getAllAction();
     $cat = array();
     foreach ($categories as $category) {
         $name = $category->getName();
         $this->addReference($name, $category);
     }
     //$this->addReference('carnes', $cat);
     $row = 0;
     $root = $this->container->getParameter('kernel.root_dir');
     $dir = str_replace('/app', '/src/DWSBundle/Resources/data/', $root);
     if (($fd = fopen($dir . "products.csv", "r")) !== FALSE) {
         while (($data = fgetcsv($fd, 1000, ",")) !== FALSE) {
             $row++;
             if ($row == 1) {
                 continue;
             }
             //skip header
             $product = new Product();
             $product->setName($data[0]);
             $product->setDescription($data[1]);
             $product->setPrice($data[3]);
             $product->setCategory($this->getReference($data[2]));
             $manager->persist($product);
             $manager->flush();
         }
         fclose($fd);
     }
 }