Databases and Symfony
Overview
Symfony standard edition comes bundled with Doctrine, a library that provides ORM and reading/writing to databases.[1]
Symfony, however, is not tied to Doctrine.
Configuring database connections
The actual database connection property values are stored in app/config/parameters.yml, which are in turn referenced in the app's main configuration file, app/config/config.yml:[2]
# app/config/config.yml
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
Using the Symfony configuration, a database can be created with
$ php app/console doctrine:database:create
Entity classes
Configuring entity classes
Classes that represent table data go in the Entity directory inside of AppBundle.[3]
Columns are represented by properties of the class. Typically these are protected and accessed with public "getter" and "setter" functions.
// src/AppBundle/Entity/Product.php
namespace AppBundle\Entity;
class Product
{
protected $id;
protected $name;
protected $description;
public function getId()
{
return ($this->id);
}
public function setID($id)
{
$this->id = $id;
}
/* etc... */
}
The getter and setter routines can be generated with
$ php app/console doctrine:generate:entities AppBundle/Entity/Product
Mapping information
Mapping metadata can be specified with YAML, XML, or directly in the entity class via annotations:[4],[5]
// src/AppBundle/Entity/Product.php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="product")
*/
class Product
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $name;
/* etc... */
}
Generating database tables and/or schema
Doctrine can create and update tables in the database using the entity classes as templates with:
$ php app/console doctrine:schema:update --force
Changes to the properties and mapping of the entity class will cause updates that will attempt to preserve the existing data.
The preferred way to generate changes that can be applied to a production environment is with migrations which generate SQL statements that are stored in migration classes.
Committing object data to a database
The logic to commit object data to a database is place in a controller:[6]
// src/AppBundle/Controller/DefaultController.php
// ...
use AppBundle\Entity\Product;
use Symfony\Component\HttpFoundation\Response;
// ...
public function createAction()
{
$product = new Product();
$product->setName('A Foo Bar');
$product->setPrice(19.95);
$product->setDescription('Lorem ipsum dolor');
$em = $this->getDoctrine()->getManager();
$em->persist($product);
$em->flush();
return new Response('Created product id '.$product->getId());
}
Retrieving data
Retrieving a single record
Repositories are used for queries for particular types of objects.[7]
$product = $this->getDoctrine()
->getRepository('AppBundle:Product')
->find($id);
if (!$product) {
throw $this->createNotFoundException(
'No product found for id '.$id
);
}
AppBundle::Product is shorthand for any class under the Entity namespace of the bundle.
Alternatively in a controller, the @ParamConverter annotation can be used to automatically load up an object using the value of an $id parameter passed to the controller.[8]
findBy(), findOneBy(), and findBy[COLUMN_NAME] are repsoitory methods used to filter queries.
Notes
- ↑ Doctrine, Symfony documentation]
- ↑ Configuring the Database, Symfony documentation
- ↑ Doctrine Documentation, Symfony Documentation, "The Book"
- ↑ Doctrine Property Mapping
- ↑ Doctrine Field Types Reference
- ↑ Persisting Objects to the Database, Doctrine documentation at Symphony documentation, "The Book"
- ↑ Fetching Objects From the Database, Symfony documentation
- ↑ FrameworkExtraBundle documentation, Symfony documentation