Databases and Symfony
Overview
Notes on how to access data in a Symfony project.
Entity classes
Configuring entity classes
Classes that represent table data go in the Entity directory inside of AppBundle.[1]
Columns are represented by properties of the class. Typically these are protected and accessed with public "getter" and "setter" functions. The getter and setter routines can be generated with
$ php app/console doctrine:generate:entities AppBundle/Entity/Product
// 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... */
}
Mapping information
Mapping metadata can be specified with YAML, XML, or directly in the entity class via annotations:[2],[3]
// 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... */
}
Notes
- ↑ Doctrine Documentation, Symfony Documentation, "The Book"
- ↑ Doctrine Property Mapping
- ↑ Doctrine Field Types Reference