Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

symfony2 -How to retrieve data from database globally in every page

I want to retrieve Cities names from a table in the database and put them as options in a select input (combobox) which is defined in 'layout.html.twig' . All my views extends 'layout.html.twig', so how can I access to cities names in every page?

[Solution]

I'm not able to respond to my topic ,I didn't have much reputation so I edit my topic

I have found the solution, using "embedding controllers"

  • first I've created an action to retreive all cities names:

    public function listCitiesAction(){
     // retreiving cities
    $entities = $this->getDoctrine()->getRepository("MedAdBundle:City")->findAll();
    
    return $this->render('MedAdBundle:Ville:list_cities.html.twig',
        array('entities' => $entities));
    
    }
    
  • this action render list_cities.html.twig defined as :

    <select class="form-control">
    {% for entity in entities %}
    <option>{{ entity.name}}</option>
    {% endfor %}
    </select>
    
  • finnaly I edit my layout.html.twig

    <div>
    {{ render(controller('MedAdBundle:City:listCities'))}}
    </div>
    

In this way I can access to cities combobox in every page in my app ;)

like image 766
user3190603 Avatar asked Jan 13 '14 15:01

user3190603


People also ask

What is Symfony and how does it work?

Symfony provides all the tools you need to use databases in your applications thanks to Doctrine, the best set of PHP libraries to work with databases. These tools support relational databases like MySQL and PostgreSQL and also NoSQL databases like MongoDB. Databases are a broad topic, so the documentation is divided in three articles:

How to validate a product entity in Symfony?

Although the Product entity doesn't define any explicit validation configuration, Symfony introspects the Doctrine mapping configuration to infer some validation rules. For example, given that the name property can't be null in the database, a NotNull constraint is added automatically to the property (if it doesn't contain that constraint already).

How do I use the Symfony profiler in SENSIO?

Click on the icon to open the Symfony Profiler and see the exact queries that were executed. If you don't see the web debug toolbar, install the profiler Symfony pack by running this command: composer require --dev symfony/profiler-pack. In many cases, you can use the SensioFrameworkExtraBundle to do the query for you automatically!

How do I install doctrine in Symfony?

First, install Doctrine support via the orm Symfony pack , as well as the MakerBundle, which will help generate some code: The database connection information is stored as an environment variable called DATABASE_URL. For development, you can find and customize this inside .env:


1 Answers

Another nice way would be use render.

This allows you to call a controller out of your layout.html.twig

{{ render(controller("AcmeDemoBundle:Helper:citySelector")) }}

you also can cache the output with ESI.

like image 51
Flask Avatar answered Oct 30 '22 21:10

Flask