public function listadoActoresAction()
{
$repository = $this->getDoctrine()->getRepository('cineactoresBundle:Actores');
$arrActores = $repository->findAll();
return $this->render('cineactoresBundle:Default:listadoActores.html.twig', array('actores' => $arrActores));
}
http://symfonylsg.dev/app_dev.php/listadoActores
Mostrar los resultados de un registro por su Id:
public function showActorPorIdAction($id)
{
$repository = $this->getDoctrine()->getRepository('cineactoresBundle:Actores');
$arrActores = $repository->findOneById($id); // solo devolverá uno
return $this->render('cineactoresBundle:Default:showActor.html.twig', array('actores' => $arrActores));
}
http://symfonylsg.dev/app_dev.php/showActorPorId/3
Mostrar los resultados de un registro filtrando por un campo concreto:
En el routing:
cineactores_showActorPorCampo:
pattern: /showActorPorCampo/{nombre_campo}/{valor}
defaults: { _controller: cineactoresBundle:Default:showActorPorCampo }
En el controller:
public function showActorPorCampoAction($nombre_campo, $valor)
{
$repository = $this->getDoctrine()->getRepository('cineactoresBundle:Actores');
$arrActores = $repository->findBy(array($nombre_campo=>$valor));
return $this->render('cineactoresBundle:Default:showActor.html.twig', array('actores' => $arrActores));
}
Ejemplos:
symfonylsg.dev/app_dev.php/showActorPorCampo/tituloPelicula/El sargento York
http://symfonylsg.dev/app_dev.php/showActorPorCampo/nombre/James%20Cagney
Las vista de showActor.html.twig sería:
{% extends 'cineactoresBundle::main.html.twig' %}
{% block title %} Mostrando detalles {%endblock%}
{% block contenido %}
{# comentario: {{ dump(actores) }} #}
<h1>Detalles del actor </h1>
{% if actores.id is defined %} {#Ha filtrado por id y solo hay un registro #}
<div><h2>{{actores.id}} - {{actores.nombre}} - {{ actores.tituloPelicula }} - {{ actores.annoOscar }}</h2> </div>
{% else %} {#Ha filtrado por un campo y devuelve varios registros #}
{% for actor in actores %}
<div><h2>{{actor.id}} - {{actor.nombre}} - {{ actor.tituloPelicula }} - {{ actor.annoOscar }}</h2> </div>
{% endfor %}
{% endif %}
{% endblock %}
{% block lateralDer %}
<a href="../listadoActores">Volver al listado</a>
{% endblock %}
No hay comentarios:
Publicar un comentario