Symfony 2 Tuto CRUD We will developed a small application symfony 2 which adds, display, update and deleting Entity : Model(id,name,country) Car(id,name,cv,idmodel) This video show how to generate (with symfony 2 command): -Bundle
-Controller -Create Entity -Foreign key many to one
-Config and create database -Create schema database (myphpadmin) -Create Form -Create Action Add,Update,delete,show
Create bundle
Create a Bundle to launch the following command in the console : Generate:bundle
And then specify the following settings:
Bundle Namespace : Tuto/ParcBundle
Bundle Name : TutoParcBundle
Target Directory : src
Configuration format : yml
Create directory structure : yes
Do you confirm generation : yes
Confirm automatic update of your Kernel : yes
Confirm automatic update of the Rooting : yes
To customize the access to a particular Bundle, need to add a prefix to the Bundle inthe Global routing file :
Projet\App\config\routing.yml
Create Controller Controller name: TutoParcBundle:Model
Routing format (php, xml, yml, annotation) [annotation]: yml
Template format (twig, php) [twig]:twig New action name (press
to stop adding actions): AddMAction Action route [/AddM]:
Templatename (optional) [TutoParcBundle:Model:AddM.html.twig]: New action name (press to stop adding actions): ListMAction Action route [/ListM]: Templatename (optional) [TutoParcBundle:Model:ListM.html.twig]:
Create Controller New action name (press to stop adding actions): UpdateMAction Action route [/UpdateM]: Templatename (optional) [TutoParcBundle:Model:UpdateM.html.twig]: New action name (press to stop adding actions): DeleteMAction Action route [/DeleteM]: Templatename (optional) [TutoParcBundle:Model:DeleteM.html.twig]: (press to stop adding actions)
Do you confirm generation [yes]: Yes
Create Entity The Entity shortcut name: TutoParcBundle:Model Configuration format (yml, xml, php, or annotation) [annotation]: annotation New field name (press to stop adding fields): name Field type [string]: Field length [255]: New field name (press to stop adding fields): country Field type [string]: Field length [255]: New field name (press to stop adding fields): Do you want to generate an empty repository class [no]? Yes Do you confirm generation [yes]? Yes Done.
Foreign Key ManyToOne
Tuto\ParcBundle\Entity\Cars @ORM\ManyToOne(targetEntity="Model")
Config And Create DataBase source file -> app -> config -> parameters. Symfony2 -> command -> create database
create schema Symfony2 -> command -> create schema
Create Form
form symfony2-> comand -> form TutoParcBundle:Cars
ListCarsAction ********* ListCars.html.twig*********
public function ListCarsAction() { $em=$this->container->get('doctrine')>getEntityManager(); $cars=$em>getRepository('TutoParcBundle:Cars')->findAll(); return $this>render('TutoParcBundle:Cars:ListCars.html.twig', array('cars'=>$cars)); }
ID | Name | CV | Model | {% for C in cars %}
{{C.id}} | {{C.name}} | {{C.idModel.name}} | Modifier | Supprimer |
{% endfor %}
AddCarsAction *********AddCars.html.twig********* public function AddCarAction() {
$cars= new cars();
{% block title %}Cars{% endblock %}
$form=$this->container->get('form.factory')->create( new CarsType(),$cars) $request=$this->getRequest(); if($request->getMethod()=='POST') {
$form->bind($request) ; if($form->isValid()) { $em=$this->container->get('doctrine')->getEntityManager(); $em->persist($cars); $em->flush(); return $this->redirect($this->generateUrl("ListCars")); }
Welcome to the Cars :Add
return $this->render('TutoParcBundle:Cars:AddCars.html.twig',
{% endblock %}
array('Form'=>$form->createView()) ); }
{% block body %}
UpdateCarsAction public function UpdateCarAction($id)
{
$em=$this->container->get('doctrine')->getEntityManager();
$cars=$em->getRepository('TutoParcBundle:Cars)->find($id)
*********UpdateCars.html.twig*********
{% block title %}Update Cars{% endblock %}
$form=$this->container->get('form.factory')->create( new CarsType(),$cars); $request=$this->getRequest();
if($request->getMethod()=='POST') {
$form->bind($request) ; if($form->isValid()) {
$em=$this->container->get('doctrine')>getEntityManager(); $em->persist($cars); $em->flush(); return $this->redirect($this->generateUrl("ListCars")); } } return $this>render(TutoParcBundle :Cars :UpdateCars.html.twig', array('Form' => $form->createView())); } }
{% block body %} Welcome to the Cars :Add
{% endblock %}
DeleteCarsAction public function DeleteCarAction($id) { $em=$this->container->get('doctrine')->getEntityManager(); $cars=$em->getRepository(TutoParcBundle :Cars')->find($id); $em->remove($cars); $em->flush(); return $this->redirect($this->generateUrl("ListCars"));