DataBases and Doctrine

  • Doctrine is totally decoupled from Symfony and using it is optional.

  • Doctrine ORM, which aims to let you map objects to a relational database (such as MySQL, PostgreSQL or Microsoft SQL).

Configuring the Database

# app/config/parameters.yml
parameters:
database_driver: pdo_mysql
database_host: localhost
database_name: test_project
database_user: root
database_password: password
#

Doctrine knows about your database, you can have it create the database for you:
$ php app/console doctrine:database:create

Note :Doctrine doesn't care whether your properties are protected or private, or whether or not you have a getter or setter function for a property. The getters and setters are generated here only because you'll need them to interact with your PHP object.

Creating the Database Tables/Schema You now have a usable Product class with mapping information so that Doctrine knows exactly how to persist it. Of course, you don't yet have the corresponding product table in your database. Fortunately Doctrine can automatically create all the database tables needed for every known entity in your application. To do this, run:

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


Persisting Objects to the Database
You have a mapped Product entity and corresponding product table, you're ready to persist data to the database. From inside a controller, this is pretty easy. Add the following method to the DefaultController of the bundle:
// src/Application/StoreBundle/Controller/DefaultController.php
use Application\StoreBundle\Entity\Product;
use Symfony\Component\HttpFoundation\Response;
public function createAction()
{
$product = new Product();
$product->setName('A Bar');
$product->setPrice('19.99');
$product->setDescription('techknow');
$em = $this->getDoctrine()->getManager();
$em->persist($product);
$em->flush();
return new Response('Created product id '.$product->getId());
}

At the previous example in more detail: lines 9-12 In this section, you instantiate and work with the $product object like any other, normal PHP object.

line 14 This line fetches Doctrine's entity manager object, which is responsible for handling the process of persisting and fetching objects to and from the database.

line 15 The persist() method tells Doctrine to "manage" the $product object. This does not actually cause a query to be made to the database (yet).

line 16 When the flush() method is called, Doctrine looks through all of the objects that it's managing to see if they need to be persisted to the database. In this example, the $product object has not been persisted yet, so the entity manager executes an INSERT query and a row is created in the product table.
Fetching Objects from the Database
Fetching an object back out of the database is even easier. For example, suppose you've configured a route to display a specific Product based on its id value :
public function showAction($id)
{
$product = $this->getDoctrine()
->getRepository('AcmeStoreBundle:Product')
->find($id);
if (!$product) {
throw $this->createNotFoundException(
'No product found for id '.$id
);
}
// ... do something, like pass the $product object into a template
}

Updating an Object
Once you've fetched an object from Doctrine, updating it is easy. Suppose you have a route that maps a product id to an update action in a controller:
public function updateAction($id)
{
$em = $this->getDoctrine()->getManager();
$product = $em->getRepository('StoreBundle:Product')->find($id);
if (!$product) {
throw $this->createNotFoundException(
'No product found for id '.$id
);
}
$product->setName('New product name!');
$em->flush();
return $this->redirect($this->generateUrl('homepage'));
}

Updating an object involves just three steps:

1. fetching the object from Doctrine.
2. modifying the object.
3. calling flush() on the entity manager
Note : Calling $em->persist($product) isn't necessary. Recall that this method simply tells Doctrine to manage or "watch" the $product object. In this case, since you fetched the $product object.

Deleting an Object

Deleting an object is very similar, but requires a call to the remove() method of the entity manager:
$em->remove($product);
$em->flush();
the remove() method notifies Doctrine that you'd like to remove the given entity from the database. The actual DELETE query, however, isn't actually executed until the flush() method is called.