ccc

Symfony BD: Ejemplo métodos básicos

<?php
namespace cine\actoresBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use cine\actoresBundle\Entity\Actores; // para trabajar con esa tabla de la BD

use Symfony\Component\HttpFoundation\Response;

class DefaultController extends Controller
{
    // *********************************
    public function indexAction($name)
    {
        return $this->render('cineactoresBundle:Default:index.html.twig', array('name' => $name));
    }
   
    // *********************************
    public function borrarActorAction($id)
    {
        $em = $this->getDoctrine()->getManager();
        $repositorio = $em->getRepository('cineactoresBundle:Actores');
        $actor = $repositorio->findOneById($id); // solo devolverá uno
       
        if (!$actor)
            throw $this->createNotFoundException('No se puede borrar porque no existe el id:'.$id);
       
        $em->remove($actor);
        $em->flush(); // lo borra de la BD
        return New Response('Se ha eliminado correctamente el actor con el id:'.$id);
    }
   
    // *********************************
    public function insertarActorAction($nombre, $anno, $tituloPelicula)
    {
        // Insertar en BD
        $actor = new Actores();
        $actor->setNombre($nombre);
        $actor->setAnnoOscar($anno);
        $actor->setTituloPelicula($tituloPelicula);
   
        $em = $this->getDoctrine()->getManager();
        $em->persist($actor);
        $em->flush();
       
        return new Response("El actor ".$nombre." se ha metido correctamente con el id:".$actor->getId());
   }
   
   // *********************************
   public function listadoActoresAction()
    {
        $em = $this->getDoctrine()->getManager();
        $repositorio = $em->getRepository('cineactoresBundle:Actores');
 
        $arrActores = $repositorio->findAll();
        return $this->render('cineactoresBundle:Default:listadoActores.html.twig', array('actores' => $arrActores));
    }
   
    // *********************************
    public function modificarActorAction($id, $nombre, $anno, $tituloPelicula)
    {
        $em = $this->getDoctrine()->getManager();
        $repositorio = $em->getRepository('cineactoresBundle:Actores');
        $actor = $repositorio->findOneById($id); // solo devolverá uno
       
        if (!$actor)
            throw $this->createNotFoundException('No se ha encontrado el actor con id:'.$id);
                  
        $actor->setNombre($nombre);
        $actor->setAnnoOscar($anno);
        $actor->setTituloPelicula($tituloPelicula);
       
        $em->persist($actor);
        $em->flush(); // lo guarda en la BD
       
        return new Response("Se ha modificado correctamente el actor con id: ".$id);
    }
   
    // *********************************
    public function showActorPorIdAction($id)
    {   
        $em = $this->getDoctrine()->getManager();
        $repositorio = $em->getRepository('cineactoresBundle:Actores');
        $arrActores = $repositorio->findOneById($id); // solo devolverá uno
        return $this->render('cineactoresBundle:Default:showActor.html.twig', array('actores' => $arrActores));
    }
   
    // *********************************
    public function showActorPorNombreAction($nombre)
    {
        $em = $this->getDoctrine()->getManager();
        $repositorio = $em->getRepository('cineactoresBundle:Actores');
         $arrActores = $repositorio->findBy(array("nombre"=>$nombre));
        return $this->render('cineactoresBundle:Default:showActor.html.twig', array('actores' => $arrActores));
       
    }
   
    // *********************************
    public function showActorPorCampoAction($nombre_campo, $valor)
    {
        $em = $this->getDoctrine()->getManager();
        $repositorio = $em->getRepository('cineactoresBundle:Actores');
        $arrActores = $repositorio->findBy(array($nombre_campo=>$valor));
        return $this->render('cineactoresBundle:Default:showActor.html.twig', array('actores' => $arrActores));
    }

}

No hay comentarios:

Publicar un comentario