Symfony 2 Tuto CRUD.pdf - Google Drive

2 downloads 108 Views 235KB Size Report
Loading… Page 1. Whoops! There was a problem loading more pages. Symfony 2 Tuto CRUD.pdf. Symfony 2 Tuto CRUD.pdf. Ope
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 )

Config And Create align="center">

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 CVModel {% 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

{{form_widget(Form)}} 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

{{form_widget(Form)}} find($id); $em->remove($cars); $em->flush(); return $this->redirect($this->generateUrl("ListCars"));