How to use Doctrine 2 in Symfony2.6

Hi guys, today I want to speak about Doctrine 2.0. In this tutorial I will explain how to create a new table and extract information from database by using the Doctrine 2.0 and Symfony 2.6.

First of all, I want to create a new table in my existing database. I open the command in my Symfony project and enter this code:

php app/console doctrine:generate:entity

Next, it ask me: The Entity shortcut name:

I want to create the table for an bundle and I will enter first the name of my bundle and the name of my table.


Next, I will enter

Configuration format (yml, xml, php, or annotation) [annotation]: yml

New field name (press <return> to stop adding fields): text(the name of second field in my table)

Field type [string]: text

Do you want to generate an empty repository class [no]? yes

Then, I will enter also this code(to update my database):

php app/console doctrine:schema:update - -force

Now I can use my create table. I will add some information in my text field of my created table.

Also, I will update my routing.yml file from my bundle with this informations:

path: /post/{id}
defaults: { _controller: NewNewBundle:Default:index }

Next, I will add this code

public function indexAction($id)
$em = $this->getDoctrine()->getManager();
$post = $em->getRepository('NewNewBundle:test')->find($id);
return $this->render('NewNewBundle:Default:index.html.twig', array('name' => $post));

in my Controller.

Next, I will add this code:

{{ name.text }} - to display the information from text field of my database.

in NewNewBundle:Default:index.html.twig

Next I will go to http://localhost/symfony2.6/web/app_dev.php/post/1 to view the first results from my table.

Video Tutorial:

Leave a Comment