Databases and Symfony

From Littledamien Wiki
Revision as of 20:54, 24 January 2015 by Video8 (talk | contribs) (Created page with "Category:Symfony category:PHP Category:Web Development == Overview == Notes on how to access data in a Symfony project. == Entity classes == === Configuring en...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


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