Reputation: 285
I'm building a page in Symfony2 that permits me to search a term (e.g. a name of a person) in my Database and show in the same page (even after a page reload) all matching records (e.g. all the person with this name).
This is my anagrafica.html.twig
{# src/Acme/MyBundle/Resources/views/Page/anagrafica.html.twig #}
{% extends 'AcmeMyBundle::layout.html.twig' %}
{% block body %}
<form id="formsearch" name="p" method="get" action="anagrafica">
<span>
<input type="text" name="search_name" id="search_name" />
</span>
<input type="image" name="button_search" />
</form>
{% for anagrafica in anagrafiche %}
<article class="blog">
<div class="date">{{ anagrafica.DataNascita|date('c') }}</div>
<header>
<h2><a href="{{ path('AcmeMyBundle_showAnag', { 'id': anagrafica.id }) }}">{{ anagrafica.nome }}</a></h2>
</header>
<div class="snippet">
<p>{{ anagrafica.cognome }}</p>
<p class="show_complete"><a href="{{ path('AcmeMyBundle_showAnag', { 'id': anagrafica.id }) }}">Show all data</a></p>
</div>
</article>
{% else %}
<p>There are no entries</p>
{% endfor %}
{% endblock %}
This is my PageController.php
<?php // src/Acme/MyBundle/Controller/PageController.php
namespace Acme\MyBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class PageController extends Controller {
//...
public function anagraficaAction() {
$em = $this->getDoctrine()
->getEntityManager();
$anagrafiche = $em->createQueryBuilder()
->select('b')
->from('AcmeMyBundle:Anagrafiche', 'b')
->where("b.nome = :nome")
->setParameter('nome', 'Alex' )
->addOrderBy('b.id', 'DESC')
->getQuery()
->getResult();
return $this->render('AcmeMyBundle:Page:anagrafica.html.twig', array('anagrafiche' => $anagrafiche));
}
}
I think I only need to update my PageController.php and replace the name 'Alex' in :
->setParameter('nome', 'Alex' )
with a variable that refers to the entry in my form definied in anagrafica.html.twig.
Anyway I have no idea of how to do this, and a quick search on google and forums do not helped me.
Any suggestion?
Upvotes: 0
Views: 1050
Reputation: 285
I found the solution. I edited my PageController.php such this:
<?php
// src/Acme/MyBundle/Controller/PageController.php
namespace Acme\MyBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class PageController extends Controller
{
public function indexAction()
{
return $this->render('AcmeMyBundle:Page:index.html.twig');
}
public function infoAction()
{
return $this->render('AcmeMyBundle:Page:info.html.twig');
}
public function anagraficaAction(Request $request)
{
$em = $this->getDoctrine()
->getEntityManager();
$title = $request->get('search_name');
$anagrafiche = $em->createQueryBuilder()
->select('b')
->from('AcmeMyBundle:Anagrafiche', 'b')
->where("b.nome = :nome")
->setParameter('nome', $title )
->addOrderBy('b.id', 'DESC')
->getQuery()
->getResult();
return $this->render('AcmeMyBundle:Page:anagrafica.html.twig', array('anagrafiche' => $anagrafiche));
}
}
Upvotes: 0
Reputation: 6691
You get GET-parameters via
$searchName = $request->query->get('search_name');
But for this you still need the $request
variable. You can use it as parameter and change your method signature to this:
public function anagraficaAction(Request $request)
This way, you can call the $request
parameter in your method.
The other way is to get the request of the current controller inside your method.
$request = $this->get('request');
Using this you can change your setParameter
to this:
setParameter('nome', $searchName)
Upvotes: 2
Reputation: 3228
change this setParameter('nome', 'Alex')
to something like setParameter('nome', $_GET['search_name'])
since this form passing data by using get method.
Upvotes: 0