From 19690dbf99c1fc9c7149546c8d424752a0fe14ec Mon Sep 17 00:00:00 2001 From: ButenkoD Date: Thu, 21 Nov 2013 16:21:10 +0200 Subject: [PATCH 1/2] Used Fixtires --- .gitignore | 1 + .gitignore~ | 2 +- app/AppKernel.php | 2 + app/Resources/views/base.html.twig | 2 +- app/config/config.yml | 9 + app/config/routing.yml | 4 + composer.json | 4 +- composer.lock | 165 +++++++++++++++++- src/Acme/StoreBundle/AcmeStoreBundle.php | 9 + .../Controller/DefaultController.php | 100 +++++++++++ .../DataFixtures/ORM/LoadCategoryData.php | 62 +++++++ .../DataFixtures/ORM/LoadProductData.php | 49 ++++++ .../StoreBundle/DataFixtures/data/product.yml | 21 +++ .../AcmeStoreExtension.php | 28 +++ .../DependencyInjection/Configuration.php | 29 +++ src/Acme/StoreBundle/Entity/Category.php | 108 ++++++++++++ src/Acme/StoreBundle/Entity/Product.php | 153 ++++++++++++++++ .../StoreBundle/Resources/config/routing.yml | 23 +++ .../StoreBundle/Resources/config/services.php | 21 +++ .../Resources/views/Default/home.html.twig | 10 ++ .../Resources/views/Default/index.html.twig | 1 + .../Resources/views/Default/layout.html.twig | 36 ++++ .../Resources/views/Default/show.html.twig | 7 + .../views/Default/showDescription.html.twig | 8 + .../Controller/DefaultControllerTest.php | 17 ++ web/.htaccess | 6 +- 26 files changed, 867 insertions(+), 10 deletions(-) create mode 100755 src/Acme/StoreBundle/AcmeStoreBundle.php create mode 100755 src/Acme/StoreBundle/Controller/DefaultController.php create mode 100755 src/Acme/StoreBundle/DataFixtures/ORM/LoadCategoryData.php create mode 100755 src/Acme/StoreBundle/DataFixtures/ORM/LoadProductData.php create mode 100755 src/Acme/StoreBundle/DataFixtures/data/product.yml create mode 100755 src/Acme/StoreBundle/DependencyInjection/AcmeStoreExtension.php create mode 100755 src/Acme/StoreBundle/DependencyInjection/Configuration.php create mode 100755 src/Acme/StoreBundle/Entity/Category.php create mode 100755 src/Acme/StoreBundle/Entity/Product.php create mode 100755 src/Acme/StoreBundle/Resources/config/routing.yml create mode 100755 src/Acme/StoreBundle/Resources/config/services.php create mode 100755 src/Acme/StoreBundle/Resources/views/Default/home.html.twig create mode 100755 src/Acme/StoreBundle/Resources/views/Default/index.html.twig create mode 100755 src/Acme/StoreBundle/Resources/views/Default/layout.html.twig create mode 100755 src/Acme/StoreBundle/Resources/views/Default/show.html.twig create mode 100644 src/Acme/StoreBundle/Resources/views/Default/showDescription.html.twig create mode 100755 src/Acme/StoreBundle/Tests/Controller/DefaultControllerTest.php diff --git a/.gitignore b/.gitignore index bbf53b3..60ed38d 100755 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ /bin/ /composer.phar /.idea/ +*~ diff --git a/.gitignore~ b/.gitignore~ index 34fb5f2..bbf53b3 100755 --- a/.gitignore~ +++ b/.gitignore~ @@ -9,4 +9,4 @@ /vendor/ /bin/ /composer.phar -/.idea/* +/.idea/ diff --git a/app/AppKernel.php b/app/AppKernel.php index c5c9eb6..c5e1e0c 100755 --- a/app/AppKernel.php +++ b/app/AppKernel.php @@ -16,6 +16,8 @@ public function registerBundles() new Symfony\Bundle\AsseticBundle\AsseticBundle(), new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(), new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(), + new Acme\StoreBundle\AcmeStoreBundle(), + new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(), ); if (in_array($this->getEnvironment(), array('dev', 'test'))) { diff --git a/app/Resources/views/base.html.twig b/app/Resources/views/base.html.twig index bafd28d..f1ef139 100755 --- a/app/Resources/views/base.html.twig +++ b/app/Resources/views/base.html.twig @@ -2,7 +2,7 @@ - {% block title %}Welcome!{% endblock %} + {% block title %}HomeWork6{% endblock %} {% block stylesheets %}{% endblock %} diff --git a/app/config/config.yml b/app/config/config.yml index 5490b59..718ef48 100755 --- a/app/config/config.yml +++ b/app/config/config.yml @@ -39,6 +39,15 @@ assetic: #yui_css: # jar: %kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar + assets: + css: + inputs: + - %kernel.root_dir%/../vendor/twbs/bootstrap/dist/css/bootstrap.css + - %kernel.root_dir%/../vendor/twbs/bootstrap/dist/css/bootstrap-theme.min.css + filters: + - cssrewrite + output: mystyle.css + # Doctrine Configuration doctrine: dbal: diff --git a/app/config/routing.yml b/app/config/routing.yml index e69de29..b9975b8 100755 --- a/app/config/routing.yml +++ b/app/config/routing.yml @@ -0,0 +1,4 @@ +acme_store: + resource: "@AcmeStoreBundle/Resources/config/routing.yml" + prefix: / + diff --git a/composer.json b/composer.json index 5705d76..dd3c3b8 100755 --- a/composer.json +++ b/composer.json @@ -18,7 +18,9 @@ "sensio/distribution-bundle": "2.3.*", "sensio/framework-extra-bundle": "2.3.*", "sensio/generator-bundle": "2.3.*", - "incenteev/composer-parameter-handler": "~2.0" + "incenteev/composer-parameter-handler": "~2.0", + "doctrine/doctrine-fixtures-bundle": "dev-master", + "twbs/bootstrap": "3.0.*@dev" }, "scripts": { "post-install-cmd": [ diff --git a/composer.lock b/composer.lock index bcedca1..31561ec 100755 --- a/composer.lock +++ b/composer.lock @@ -3,7 +3,7 @@ "This file locks the dependencies of your project to a known state", "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file" ], - "hash": "e67df720e8a1a2e0e6df41d91d788e48", + "hash": "160a997aed852357e8004e54fbe83f98", "packages": [ { "name": "doctrine/annotations", @@ -294,6 +294,62 @@ ], "time": "2013-09-07 10:20:34" }, + { + "name": "doctrine/data-fixtures", + "version": "v1.0.0", + "source": { + "type": "git", + "url": "https://github.com/doctrine/data-fixtures.git", + "reference": "b4a135c7db56ecc4602b54a2184368f440cac33e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/data-fixtures/zipball/b4a135c7db56ecc4602b54a2184368f440cac33e", + "reference": "b4a135c7db56ecc4602b54a2184368f440cac33e", + "shasum": "" + }, + "require": { + "doctrine/common": ">=2.2,<2.5-dev", + "php": ">=5.3.2" + }, + "require-dev": { + "doctrine/orm": ">=2.2,<2.5-dev" + }, + "suggest": { + "doctrine/mongodb-odm": "For loading MongoDB ODM fixtures", + "doctrine/orm": "For loading ORM fixtures", + "doctrine/phpcr-odm": "For loading PHPCR ODM fixtures" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-0": { + "Doctrine\\Common\\DataFixtures": "lib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com", + "homepage": "http://www.jwage.com/", + "role": "Creator" + } + ], + "description": "Data Fixtures for all Doctrine Object Managers", + "homepage": "http://www.doctrine-project.org", + "keywords": [ + "database" + ], + "time": "2013-07-10 17:04:07" + }, { "name": "doctrine/dbal", "version": "2.3.4", @@ -428,6 +484,64 @@ ], "time": "2013-03-25 20:13:59" }, + { + "name": "doctrine/doctrine-fixtures-bundle", + "version": "dev-master", + "target-dir": "Doctrine/Bundle/FixturesBundle", + "source": { + "type": "git", + "url": "https://github.com/doctrine/DoctrineFixturesBundle.git", + "reference": "3caec4826e58fe47520b591f965b4adcdfc8a6c8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/DoctrineFixturesBundle/zipball/3caec4826e58fe47520b591f965b4adcdfc8a6c8", + "reference": "3caec4826e58fe47520b591f965b4adcdfc8a6c8", + "shasum": "" + }, + "require": { + "doctrine/data-fixtures": "~1.0", + "doctrine/doctrine-bundle": "~1.0", + "php": ">=5.3.2", + "symfony/doctrine-bridge": "~2.1" + }, + "type": "symfony-bundle", + "extra": { + "branch-alias": { + "dev-master": "2.2.x-dev" + } + }, + "autoload": { + "psr-0": { + "Doctrine\\Bundle\\FixturesBundle": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "http://symfony.com/contributors" + }, + { + "name": "Doctrine Project", + "homepage": "http://www.doctrine-project.org" + } + ], + "description": "Symfony DoctrineFixturesBundle", + "homepage": "http://www.doctrine-project.org", + "keywords": [ + "Fixture", + "persistence" + ], + "time": "2013-09-16 10:15:05" + }, { "name": "doctrine/inflector", "version": "v1.0", @@ -1413,6 +1527,48 @@ ], "time": "2013-11-14 07:58:51" }, + { + "name": "twbs/bootstrap", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/twbs/bootstrap.git", + "reference": "6ed5a844a8ca3582240ddda70d63a3d7bfea7c50" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/twbs/bootstrap/zipball/6ed5a844a8ca3582240ddda70d63a3d7bfea7c50", + "reference": "6ed5a844a8ca3582240ddda70d63a3d7bfea7c50", + "shasum": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0.x-dev" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "Jacob Thornton", + "email": "jacobthornton@gmail.com" + }, + { + "name": "Mark Otto", + "email": "markdotto@gmail.com" + } + ], + "description": "Sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development.", + "homepage": "http://getbootstrap.com", + "keywords": [ + "bootstrap", + "css" + ], + "time": "2013-11-19 22:15:09" + }, { "name": "twig/extensions", "version": "v1.0.1", @@ -1517,9 +1673,10 @@ ], "minimum-stability": "stable", - "stability-flags": [ - - ], + "stability-flags": { + "doctrine/doctrine-fixtures-bundle": 20, + "twbs/bootstrap": 20 + }, "platform": { "php": ">=5.3.3" }, diff --git a/src/Acme/StoreBundle/AcmeStoreBundle.php b/src/Acme/StoreBundle/AcmeStoreBundle.php new file mode 100755 index 0000000..1df80e4 --- /dev/null +++ b/src/Acme/StoreBundle/AcmeStoreBundle.php @@ -0,0 +1,9 @@ +render('AcmeStoreBundle:Default:home.html.twig', array('tasklist' => $taskArray)); + } + + public function createAction() + { + $em = $this->getDoctrine()->getManager(); + + $product = new Product(); + $product->setName("Guitar4"); + $product->setPrice(888); + $product->setDescription("To play black metal for the Satan's glory"); + + $category = $em + ->getRepository("AcmeStoreBundle:Category") + ->findOneBy(array('id'=>2)); + + $product->setCategory($category); + + $em->persist($product); + $em->persist($category); + $em->flush(); + + return new Response('item #'.$product->getId()." was added to ".$category->getId().'th category' ); + #$this->render('AcmeStoreBundle:Default:index.html.twig', array('name' => $name)); + } + + public function showAction($id) + { + $repository = $this->getDoctrine() + ->getRepository("AcmeStoreBundle:Product"); + + $product = $repository + ->findOneBy(array('id'=>$id)); + + if(!$product) { + throw $this->createNotFoundException( + "Couldn't find item" + ); + } + + return $this->render('AcmeStoreBundle:Default:show.html.twig', array( + 'product' => $product, + 'category' => $product->getCategory() + )); + } + + public function showDescriptionAction($id) + { + $repository = $this->getDoctrine() + ->getRepository("AcmeStoreBundle:Product"); + + $product = $repository + ->findOneBy(array('id'=>$id)); + + return $this->render('AcmeStoreBundle:Default:showDescription.html.twig', array('product' => $product)); + } + + public function updateAction($id) + { + $em = $this->getDoctrine()->getManager(); + $product = $em->getRepository("AcmeStoreBundle:Product") + ->find($id); + $product->setName("Booze"); + $em->flush(); + + #return new Response('updated'); + return $this->redirect($this->generateURL("_show_route", array("id" => $id))); + } + + public function removeAction($id) + { + + $em = $this->getDoctrine()->getManager(); + $product = $em->getRepository("AcmeStoreBundle:Product") + ->find($id); + $em->remove($product); + $em->flush(); + + return new Response('Deleted'); + } +} diff --git a/src/Acme/StoreBundle/DataFixtures/ORM/LoadCategoryData.php b/src/Acme/StoreBundle/DataFixtures/ORM/LoadCategoryData.php new file mode 100755 index 0000000..b6f647d --- /dev/null +++ b/src/Acme/StoreBundle/DataFixtures/ORM/LoadCategoryData.php @@ -0,0 +1,62 @@ +getCategoryArray() as $categoryName){ + $categoryObject = new Category(); + $categoryObject->setName($categoryName); + #$this->setReference($categoryObject->getId(), $categoryObject); + $manager->persist($categoryObject); + }*/ + $categoryObject1 = new Category(); + $categoryObject1->setName("Guitar"); + $this->setReference('1',$categoryObject1); + $manager->persist($categoryObject1); + + $categoryObject2 = new Category(); + $categoryObject2->setName("Drums"); + $this->setReference('2',$categoryObject2); + $manager->persist($categoryObject2); + + $categoryObject3 = new Category(); + $categoryObject3->setName("Keyboard"); + $this->setReference('3',$categoryObject3); + $manager->persist($categoryObject3); + + + $manager->flush(); + } + + /**public function getCategoryArray() + { + return array( + 'Guitar', + 'Drums', + 'Keyboard' + ); + }*/ + + public function getOrder() + { + return 1; + } +} \ No newline at end of file diff --git a/src/Acme/StoreBundle/DataFixtures/ORM/LoadProductData.php b/src/Acme/StoreBundle/DataFixtures/ORM/LoadProductData.php new file mode 100755 index 0000000..3c5ad0e --- /dev/null +++ b/src/Acme/StoreBundle/DataFixtures/ORM/LoadProductData.php @@ -0,0 +1,49 @@ +getProductFile()); + + foreach($productArray['products'] as $productItem){ + $productObject = new Product(); + + $productObject + ->setName($productItem['name']) + ->setPrice($productItem['price']) + ->setDescription($productItem['description']) + ->setCategory($this->getReference((int)$productItem['category'])); + + $manager->persist($productObject); + } + $manager->flush(); + } + + public function getProductFile() + { + return __DIR__.'/../data/product.yml'; + } + + public function getOrder() + { + return 2; + } +} \ No newline at end of file diff --git a/src/Acme/StoreBundle/DataFixtures/data/product.yml b/src/Acme/StoreBundle/DataFixtures/data/product.yml new file mode 100755 index 0000000..446b33d --- /dev/null +++ b/src/Acme/StoreBundle/DataFixtures/data/product.yml @@ -0,0 +1,21 @@ +products: + Gibson: + name: Gibson + price: 1000 + description: To play rock + category: 1 + Jackson: + name: Jackson + price: 1200 + description: To play metal + category: 1 + Tama: + name: Tama + price: 500 + description: To play anything you want + category: 2 + Some_keys: + name: KeyBoard1 + price: 400 + description: To play anything you want + category: 3 \ No newline at end of file diff --git a/src/Acme/StoreBundle/DependencyInjection/AcmeStoreExtension.php b/src/Acme/StoreBundle/DependencyInjection/AcmeStoreExtension.php new file mode 100755 index 0000000..e0b2f16 --- /dev/null +++ b/src/Acme/StoreBundle/DependencyInjection/AcmeStoreExtension.php @@ -0,0 +1,28 @@ +processConfiguration($configuration, $configs); + + $loader = new Loader\PhpFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); + $loader->load('services.php'); + } +} diff --git a/src/Acme/StoreBundle/DependencyInjection/Configuration.php b/src/Acme/StoreBundle/DependencyInjection/Configuration.php new file mode 100755 index 0000000..85bc4cb --- /dev/null +++ b/src/Acme/StoreBundle/DependencyInjection/Configuration.php @@ -0,0 +1,29 @@ +root('acme_store'); + + // Here you should define the parameters that are allowed to + // configure your bundle. See the documentation linked above for + // more information on that topic. + + return $treeBuilder; + } +} diff --git a/src/Acme/StoreBundle/Entity/Category.php b/src/Acme/StoreBundle/Entity/Category.php new file mode 100755 index 0000000..5336df6 --- /dev/null +++ b/src/Acme/StoreBundle/Entity/Category.php @@ -0,0 +1,108 @@ +products = new ArrayCollection(); + } + + /** + * Get id + * + * @return integer + */ + public function getId() + { + return $this->id; + } + + /** + * Set name + * + * @param string $name + * @return Category + */ + public function setName($name) + { + $this->name = $name; + + return $this; + } + + /** + * Get name + * + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * Add products + * + * @param \Acme\StoreBundle\Entity\Product $products + * @return Category + */ + public function addProduct(\Acme\StoreBundle\Entity\Product $products) + { + $this->products[] = $products; + + return $this; + } + + /** + * Remove products + * + * @param \Acme\StoreBundle\Entity\Product $products + */ + public function removeProduct(\Acme\StoreBundle\Entity\Product $products) + { + $this->products->removeElement($products); + } + + /** + * Get products + * + * @return \Doctrine\Common\Collections\Collection + */ + public function getProducts() + { + return $this->products; + } +} \ No newline at end of file diff --git a/src/Acme/StoreBundle/Entity/Product.php b/src/Acme/StoreBundle/Entity/Product.php new file mode 100755 index 0000000..a66c6e2 --- /dev/null +++ b/src/Acme/StoreBundle/Entity/Product.php @@ -0,0 +1,153 @@ +id; + } + + /** + * Set name + * + * @param string $name + * @return Product + */ + public function setName($name) + { + $this->name = $name; + + return $this; + } + + /** + * Get name + * + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * Set price + * + * @param float $price + * @return Product + */ + public function setPrice($price) + { + $this->price = $price; + + return $this; + } + + /** + * Get price + * + * @return float + */ + public function getPrice() + { + return $this->price; + } + + /** + * Set description + * + * @param string $description + * @return Product + */ + public function setDescription($description) + { + $this->description = $description; + + return $this; + } + + /** + * Get description + * + * @return string + */ + public function getDescription() + { + return $this->description; + } + + /** + * Set category + * + * @param \Acme\StoreBundle\Entity\Category $category + * @return Product + */ + public function setCategory(\Acme\StoreBundle\Entity\Category $category = null) + { + $this->category = $category; + + return $this; + } + + /** + * Get category + * + * @return \Acme\StoreBundle\Entity\Category + */ + public function getCategory() + { + return $this->category; + } +} \ No newline at end of file diff --git a/src/Acme/StoreBundle/Resources/config/routing.yml b/src/Acme/StoreBundle/Resources/config/routing.yml new file mode 100755 index 0000000..3b76709 --- /dev/null +++ b/src/Acme/StoreBundle/Resources/config/routing.yml @@ -0,0 +1,23 @@ +_home_route: + pattern: / + defaults: { _controller: AcmeStoreBundle:Default:home } + +_create_route: + pattern: /create + defaults: { _controller: AcmeStoreBundle:Default:create } + +_show_route: + pattern: /show/{id} + defaults: { _controller: AcmeStoreBundle:Default:show, id: 1 } + +_show_description_route: + pattern: /showdescription/{id} + defaults: { _controller: AcmeStoreBundle:Default:showDescription, id: 1 } + +_update_route: + pattern: /update/{id} + defaults: { _controller: AcmeStoreBundle:Default:update } + +_remove_route: + pattern: /remove/{id} + defaults: { _controller: AcmeStoreBundle:Default:remove } \ No newline at end of file diff --git a/src/Acme/StoreBundle/Resources/config/services.php b/src/Acme/StoreBundle/Resources/config/services.php new file mode 100755 index 0000000..001e497 --- /dev/null +++ b/src/Acme/StoreBundle/Resources/config/services.php @@ -0,0 +1,21 @@ +setDefinition( + 'acme_store.example', + new Definition( + 'Acme\StoreBundle\Example', + array( + new Reference('service_id'), + "plain_value", + new Parameter('parameter_name'), + ) + ) +); + +*/ \ No newline at end of file diff --git a/src/Acme/StoreBundle/Resources/views/Default/home.html.twig b/src/Acme/StoreBundle/Resources/views/Default/home.html.twig new file mode 100755 index 0000000..fdbc154 --- /dev/null +++ b/src/Acme/StoreBundle/Resources/views/Default/home.html.twig @@ -0,0 +1,10 @@ +{% extends 'AcmeStoreBundle:Default:layout.html.twig' %} + +{% block content %} +
+ Tasks + {% for task in tasklist %} +

{{ task }}

+ {% endfor %} +
+{% endblock %} \ No newline at end of file diff --git a/src/Acme/StoreBundle/Resources/views/Default/index.html.twig b/src/Acme/StoreBundle/Resources/views/Default/index.html.twig new file mode 100755 index 0000000..3cbd38c --- /dev/null +++ b/src/Acme/StoreBundle/Resources/views/Default/index.html.twig @@ -0,0 +1 @@ +{{ object }} diff --git a/src/Acme/StoreBundle/Resources/views/Default/layout.html.twig b/src/Acme/StoreBundle/Resources/views/Default/layout.html.twig new file mode 100755 index 0000000..09cea86 --- /dev/null +++ b/src/Acme/StoreBundle/Resources/views/Default/layout.html.twig @@ -0,0 +1,36 @@ +{% extends "::base.html.twig" %} + +{% block stylesheets %} + +{% endblock %} + +{% block body %} + {% block navbar %} + + {% endblock %} + {% block content %}{% endblock %} +{% endblock %} + +{% block javascripts %} + +{% endblock %} \ No newline at end of file diff --git a/src/Acme/StoreBundle/Resources/views/Default/show.html.twig b/src/Acme/StoreBundle/Resources/views/Default/show.html.twig new file mode 100755 index 0000000..a72e9d8 --- /dev/null +++ b/src/Acme/StoreBundle/Resources/views/Default/show.html.twig @@ -0,0 +1,7 @@ +{% extends 'AcmeStoreBundle:Default:layout.html.twig' %} + +{% block content %} +
+

Product #{{ product.id }} {{ product.name }} belongs to {{ category.name }} category

+
+{% endblock %} \ No newline at end of file diff --git a/src/Acme/StoreBundle/Resources/views/Default/showDescription.html.twig b/src/Acme/StoreBundle/Resources/views/Default/showDescription.html.twig new file mode 100644 index 0000000..db8cced --- /dev/null +++ b/src/Acme/StoreBundle/Resources/views/Default/showDescription.html.twig @@ -0,0 +1,8 @@ +{% extends 'AcmeStoreBundle:Default:layout.html.twig' %} + +{% block content %} +
+

Here is description for {{ product.name }}:

+

{{ product.description }}

+
+{% endblock %} \ No newline at end of file diff --git a/src/Acme/StoreBundle/Tests/Controller/DefaultControllerTest.php b/src/Acme/StoreBundle/Tests/Controller/DefaultControllerTest.php new file mode 100755 index 0000000..477d487 --- /dev/null +++ b/src/Acme/StoreBundle/Tests/Controller/DefaultControllerTest.php @@ -0,0 +1,17 @@ +request('GET', '/hello/Fabien'); + + $this->assertTrue($crawler->filter('html:contains("Hello Fabien")')->count() > 0); + } +} diff --git a/web/.htaccess b/web/.htaccess index 0c5f37d..d0c145b 100755 --- a/web/.htaccess +++ b/web/.htaccess @@ -3,7 +3,7 @@ # mod_rewrite). Additionally, this reduces the matching process for the # start page (path "/") because otherwise Apache will apply the rewriting rules # to each configured DirectoryIndex file (e.g. index.php, index.html, index.pl). -DirectoryIndex app.php +DirectoryIndex app_dev.php RewriteEngine On @@ -38,7 +38,7 @@ DirectoryIndex app.php RewriteRule .? - [L] # Rewrite all other queries to the front controller. - RewriteRule .? %{ENV:BASE}/app.php [L] + RewriteRule .? %{ENV:BASE}/app_dev.php [L] @@ -46,7 +46,7 @@ DirectoryIndex app.php # When mod_rewrite is not available, we instruct a temporary redirect of # the start page to the front controller explicitly so that the website # and the generated links can still be used. - RedirectMatch 302 ^/$ /app.php/ + RedirectMatch 302 ^/$ /app_dev.php/ # RedirectTemp cannot be used instead From 830da92c79d14b006a93fa5e6b0c7a61a5624ad6 Mon Sep 17 00:00:00 2001 From: ButenkoD Date: Wed, 27 Nov 2013 17:52:01 +0200 Subject: [PATCH 2/2] ManyToMany added --- .../Controller/DefaultController.php | 115 ++++++++++++------ .../DataFixtures/ORM/LoadColorData.php | 53 ++++++++ .../DataFixtures/ORM/LoadProductData.php | 5 +- .../StoreBundle/DataFixtures/data/product.yml | 13 +- src/Acme/StoreBundle/Entity/Color.php | 101 +++++++++++++++ src/Acme/StoreBundle/Entity/Product.php | 39 ++++++ .../StoreBundle/Resources/config/routing.yml | 18 +-- .../Resources/views/Default/layout.html.twig | 11 +- .../Resources/views/Default/show.html.twig | 5 +- .../views/Default/showColor.html.twig | 13 ++ .../views/Default/showDescription.html.twig | 5 +- .../Resources/views/Default/tasks.html.twig | 10 ++ 12 files changed, 332 insertions(+), 56 deletions(-) create mode 100755 src/Acme/StoreBundle/DataFixtures/ORM/LoadColorData.php create mode 100644 src/Acme/StoreBundle/Entity/Color.php create mode 100644 src/Acme/StoreBundle/Resources/views/Default/showColor.html.twig create mode 100755 src/Acme/StoreBundle/Resources/views/Default/tasks.html.twig diff --git a/src/Acme/StoreBundle/Controller/DefaultController.php b/src/Acme/StoreBundle/Controller/DefaultController.php index e5d22fd..29a51e3 100755 --- a/src/Acme/StoreBundle/Controller/DefaultController.php +++ b/src/Acme/StoreBundle/Controller/DefaultController.php @@ -9,7 +9,7 @@ class DefaultController extends Controller { - public function homeAction() + public function tasksAction() { $taskArray = array( 'Research about DB', @@ -17,7 +17,7 @@ public function homeAction() 'Use Fixtures' ); - return $this->render('AcmeStoreBundle:Default:home.html.twig', array('tasklist' => $taskArray)); + return $this->render('AcmeStoreBundle:Default:tasks.html.twig', array('tasklist' => $taskArray)); } public function createAction() @@ -25,22 +25,25 @@ public function createAction() $em = $this->getDoctrine()->getManager(); $product = new Product(); - $product->setName("Guitar4"); - $product->setPrice(888); - $product->setDescription("To play black metal for the Satan's glory"); + $product->setName("Ibanez") + ->setPrice(888) + ->setDescription("To play metal (nu-metal)"); $category = $em ->getRepository("AcmeStoreBundle:Category") - ->findOneBy(array('id'=>2)); + ->findOneBy(array('id'=>1)); + $color = $em + ->getRepository("AcmeStoreBundle:Color") + ->findOneBy(array('id'=>3)); - $product->setCategory($category); + $product->setCategory($category) + ->addColor($color); $em->persist($product); - $em->persist($category); $em->flush(); - return new Response('item #'.$product->getId()." was added to ".$category->getId().'th category' ); - #$this->render('AcmeStoreBundle:Default:index.html.twig', array('name' => $name)); + return $this->redirect($this->generateURL('_show_route', array('id' => $product->getId()))); + #return $this->showAction($product->getId()); } public function showAction($id) @@ -48,19 +51,16 @@ public function showAction($id) $repository = $this->getDoctrine() ->getRepository("AcmeStoreBundle:Product"); - $product = $repository - ->findOneBy(array('id'=>$id)); - - if(!$product) { - throw $this->createNotFoundException( - "Couldn't find item" - ); + if(isset($id)){ + $product[] = $repository + ->findOneBy(array('id'=>$id)); + } + else { + $product = $repository + ->findAll(); } - return $this->render('AcmeStoreBundle:Default:show.html.twig', array( - 'product' => $product, - 'category' => $product->getCategory() - )); + return $this->render('AcmeStoreBundle:Default:show.html.twig', array('products' => $product)); } public function showDescriptionAction($id) @@ -68,33 +68,68 @@ public function showDescriptionAction($id) $repository = $this->getDoctrine() ->getRepository("AcmeStoreBundle:Product"); - $product = $repository - ->findOneBy(array('id'=>$id)); + if(isset($id)) { + $product[] = $repository + ->findOneBy(array('id'=>$id)); + } + else { + $product = $repository + ->findAll(); + } - return $this->render('AcmeStoreBundle:Default:showDescription.html.twig', array('product' => $product)); + return $this->render('AcmeStoreBundle:Default:showDescription.html.twig', array('products' => $product)); } - public function updateAction($id) + public function showColorAction($id) { - $em = $this->getDoctrine()->getManager(); - $product = $em->getRepository("AcmeStoreBundle:Product") - ->find($id); - $product->setName("Booze"); - $em->flush(); + $repository = $this->getDoctrine() + ->getRepository("AcmeStoreBundle:Product"); - #return new Response('updated'); - return $this->redirect($this->generateURL("_show_route", array("id" => $id))); + if(isset($id)) { + $product[] = $repository + ->findOneBy(array('id'=>$id)); + } + else { + $product = $repository + ->findAll(); + } + + return $this->render('AcmeStoreBundle:Default:showColor.html.twig', array('products' => $product)); } - public function removeAction($id) + public function updateAction($id) { + if(!isset($id)){ + throw $this->createNotFoundException( + 'You must set "id" in URL' + ); + } + else { + $em = $this->getDoctrine()->getManager(); + $product = $em->getRepository("AcmeStoreBundle:Product") + ->find($id); + $product->setName("Booze"); + $em->flush(); + + return $this->redirect($this->generateURL("_show_route", array("id" => $id))); + } + } - $em = $this->getDoctrine()->getManager(); - $product = $em->getRepository("AcmeStoreBundle:Product") - ->find($id); - $em->remove($product); - $em->flush(); - - return new Response('Deleted'); + public function removeAction($id) + { + if(!isset($id)){ + throw $this->createNotFoundException( + 'You must set "id" in URL' + ); + } + else { + $em = $this->getDoctrine()->getManager(); + $product = $em->getRepository("AcmeStoreBundle:Product") + ->find($id); + $em->remove($product); + $em->flush(); + + return $this->redirect($this->generateURL('_show_route')); + } } } diff --git a/src/Acme/StoreBundle/DataFixtures/ORM/LoadColorData.php b/src/Acme/StoreBundle/DataFixtures/ORM/LoadColorData.php new file mode 100755 index 0000000..6edf374 --- /dev/null +++ b/src/Acme/StoreBundle/DataFixtures/ORM/LoadColorData.php @@ -0,0 +1,53 @@ +getCategoryArray() as $categoryName){ + $categoryObject = new Category(); + $categoryObject->setName($categoryName); + #$this->setReference($categoryObject->getId(), $categoryObject); + $manager->persist($categoryObject); + }*/ + $colorObject1 = new Color(); + $colorObject1->setColor("black"); + $this->setReference('b',$colorObject1); + $manager->persist($colorObject1); + + $colorObject2 = new Color(); + $colorObject2->setColor("white"); + $this->setReference('w',$colorObject2); + $manager->persist($colorObject2); + + $colorObject3 = new Color(); + $colorObject3->setColor("red"); + $this->setReference('r',$colorObject3); + $manager->persist($colorObject3); + + + $manager->flush(); + } + + public function getOrder() + { + return 2; + } +} \ No newline at end of file diff --git a/src/Acme/StoreBundle/DataFixtures/ORM/LoadProductData.php b/src/Acme/StoreBundle/DataFixtures/ORM/LoadProductData.php index 3c5ad0e..bf935e6 100755 --- a/src/Acme/StoreBundle/DataFixtures/ORM/LoadProductData.php +++ b/src/Acme/StoreBundle/DataFixtures/ORM/LoadProductData.php @@ -31,6 +31,9 @@ public function load(ObjectManager $manager) ->setPrice($productItem['price']) ->setDescription($productItem['description']) ->setCategory($this->getReference((int)$productItem['category'])); + foreach($productItem['colors'] as $color){ + $productObject->addColor($this->getReference($color)); + } $manager->persist($productObject); } @@ -44,6 +47,6 @@ public function getProductFile() public function getOrder() { - return 2; + return 3; } } \ No newline at end of file diff --git a/src/Acme/StoreBundle/DataFixtures/data/product.yml b/src/Acme/StoreBundle/DataFixtures/data/product.yml index 446b33d..d2bea66 100755 --- a/src/Acme/StoreBundle/DataFixtures/data/product.yml +++ b/src/Acme/StoreBundle/DataFixtures/data/product.yml @@ -4,18 +4,29 @@ products: price: 1000 description: To play rock category: 1 + colors: + - b + - w + Jackson: name: Jackson price: 1200 description: To play metal category: 1 + colors: + - w Tama: name: Tama price: 500 description: To play anything you want category: 2 + colors: + - r + - b Some_keys: name: KeyBoard1 price: 400 description: To play anything you want - category: 3 \ No newline at end of file + category: 3 + colors: + - r \ No newline at end of file diff --git a/src/Acme/StoreBundle/Entity/Color.php b/src/Acme/StoreBundle/Entity/Color.php new file mode 100644 index 0000000..78dd8c8 --- /dev/null +++ b/src/Acme/StoreBundle/Entity/Color.php @@ -0,0 +1,101 @@ +products = new ArrayCollection(); + } + + /** + * Get id + * + * @return integer + */ + public function getId() + { + return $this->id; + } + + /** + * Set color + * + * @param string $color + * @return Color + */ + public function setColor($color) + { + $this->color = $color; + + return $this; + } + + /** + * Get color + * + * @return string + */ + public function getColor() + { + return $this->color; + } + + /** + * Set products + * + * @param Product + * @return Color + */ + public function addProducts($products) + { + $this->products[] = $products; + + return $this; + } + + /** + * Get products + * + * @return array + */ + public function getProducts() + { + return $this->products; + } +} diff --git a/src/Acme/StoreBundle/Entity/Product.php b/src/Acme/StoreBundle/Entity/Product.php index a66c6e2..639452b 100755 --- a/src/Acme/StoreBundle/Entity/Product.php +++ b/src/Acme/StoreBundle/Entity/Product.php @@ -2,6 +2,7 @@ namespace Acme\StoreBundle\Entity; +use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; /** @@ -48,6 +49,44 @@ class Product */ private $category; + /** + * @ORM\ManyToMany(targetEntity="Color", inversedBy="products") + * @ORM\JoinTable(name="products_colors") + */ + private $colors; + + public function __construct() + { + $this->colors = new ArrayCollection; + } + + /** + * @param $Color + * @return Color + */ + public function addColor($color) + { + $this->colors[] = $color; + + return $color; + } + + /** + * + */ + public function removeColor($color) + { + $this->colors->removeElement($color); + } + + /** + * @return mixed + */ + public function getColors() + { + return $this->colors; + } + /** * Get id diff --git a/src/Acme/StoreBundle/Resources/config/routing.yml b/src/Acme/StoreBundle/Resources/config/routing.yml index 3b76709..2487fcc 100755 --- a/src/Acme/StoreBundle/Resources/config/routing.yml +++ b/src/Acme/StoreBundle/Resources/config/routing.yml @@ -1,6 +1,6 @@ -_home_route: +_tasks_route: pattern: / - defaults: { _controller: AcmeStoreBundle:Default:home } + defaults: { _controller: AcmeStoreBundle:Default:tasks } _create_route: pattern: /create @@ -8,16 +8,20 @@ _create_route: _show_route: pattern: /show/{id} - defaults: { _controller: AcmeStoreBundle:Default:show, id: 1 } + defaults: { _controller: AcmeStoreBundle:Default:show, id: null } _show_description_route: - pattern: /showdescription/{id} - defaults: { _controller: AcmeStoreBundle:Default:showDescription, id: 1 } + pattern: /show_description/{id} + defaults: { _controller: AcmeStoreBundle:Default:showDescription, id: null} + +_show_color_route: + pattern: /show_color/{id} + defaults: { _controller: AcmeStoreBundle:Default:showColor, id: null} _update_route: pattern: /update/{id} - defaults: { _controller: AcmeStoreBundle:Default:update } + defaults: { _controller: AcmeStoreBundle:Default:update, id: null } _remove_route: pattern: /remove/{id} - defaults: { _controller: AcmeStoreBundle:Default:remove } \ No newline at end of file + defaults: { _controller: AcmeStoreBundle:Default:remove, id: null } \ No newline at end of file diff --git a/src/Acme/StoreBundle/Resources/views/Default/layout.html.twig b/src/Acme/StoreBundle/Resources/views/Default/layout.html.twig index 09cea86..903aeb0 100755 --- a/src/Acme/StoreBundle/Resources/views/Default/layout.html.twig +++ b/src/Acme/StoreBundle/Resources/views/Default/layout.html.twig @@ -15,14 +15,17 @@ - Project name + HomeWork diff --git a/src/Acme/StoreBundle/Resources/views/Default/show.html.twig b/src/Acme/StoreBundle/Resources/views/Default/show.html.twig index a72e9d8..b90ea10 100755 --- a/src/Acme/StoreBundle/Resources/views/Default/show.html.twig +++ b/src/Acme/StoreBundle/Resources/views/Default/show.html.twig @@ -2,6 +2,9 @@ {% block content %}
-

Product #{{ product.id }} {{ product.name }} belongs to {{ category.name }} category

+ {#

Product #{{ product.id }} {{ product.name }} belongs to {{ category.name }} category

#} + {% for product in products %} +

Product #{{ product.id }} {{ product.name }} belongs to {{ product.getCategory().getName }} category

+ {% endfor %}
{% endblock %} \ No newline at end of file diff --git a/src/Acme/StoreBundle/Resources/views/Default/showColor.html.twig b/src/Acme/StoreBundle/Resources/views/Default/showColor.html.twig new file mode 100644 index 0000000..9c0419d --- /dev/null +++ b/src/Acme/StoreBundle/Resources/views/Default/showColor.html.twig @@ -0,0 +1,13 @@ +{% extends 'AcmeStoreBundle:Default:layout.html.twig' %} + +{% block content %} +
+ {% for product in products %} +

{{ product.name }} is + {% for color in product.getColors() %} + {{ color.getColor() }} + {% endfor %} +

+ {% endfor %} +
+{% endblock %} \ No newline at end of file diff --git a/src/Acme/StoreBundle/Resources/views/Default/showDescription.html.twig b/src/Acme/StoreBundle/Resources/views/Default/showDescription.html.twig index db8cced..bc9ed55 100644 --- a/src/Acme/StoreBundle/Resources/views/Default/showDescription.html.twig +++ b/src/Acme/StoreBundle/Resources/views/Default/showDescription.html.twig @@ -2,7 +2,8 @@ {% block content %}
-

Here is description for {{ product.name }}:

-

{{ product.description }}

+ {% for product in products %} +

{{ product.name }}'s description: {{ product.description }}

+ {% endfor %}
{% endblock %} \ No newline at end of file diff --git a/src/Acme/StoreBundle/Resources/views/Default/tasks.html.twig b/src/Acme/StoreBundle/Resources/views/Default/tasks.html.twig new file mode 100755 index 0000000..fdbc154 --- /dev/null +++ b/src/Acme/StoreBundle/Resources/views/Default/tasks.html.twig @@ -0,0 +1,10 @@ +{% extends 'AcmeStoreBundle:Default:layout.html.twig' %} + +{% block content %} +
+ Tasks + {% for task in tasklist %} +

{{ task }}

+ {% endfor %} +
+{% endblock %} \ No newline at end of file