Databases and Symfony: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
(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...")
 
No edit summary
Line 11: Line 11:
Classes that represent table data go in the `Entity` directory inside of `AppBundle`.<ref>[http://symfony.com/doc/current/book/doctrine.html Doctrine Documentation], Symfony Documentation, "The Book"</ref>
Classes that represent table data go in the `Entity` directory inside of `AppBundle`.<ref>[http://symfony.com/doc/current/book/doctrine.html Doctrine Documentation], Symfony Documentation, "The Book"</ref>


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
Columns are represented by properties of the class. Typically these are `protected` and accessed with `public` "getter" and "setter" functions.  
 
<syntaxhighlight lang="bash">
$ php app/console doctrine:generate:entities AppBundle/Entity/Product
</syntaxhighlight>


<syntaxhighlight lang="php" highlight="6-8,10,15">
<syntaxhighlight lang="php" highlight="6-8,10,15">
Line 39: Line 35:
/* etc... */  
/* etc... */  
}
}
</syntaxhighlight>
The getter and setter routines can be generated with
<syntaxhighlight lang="bash">
$ php app/console doctrine:generate:entities AppBundle/Entity/Product
</syntaxhighlight>
</syntaxhighlight>


Line 70: Line 72:


/* etc... */
/* etc... */
}
</syntaxhighlight>
=== Generating database tables and/or schema ===
Doctrine can create and update tables in the database using the entity classes as templates with:
<syntaxhighlight lang="bash">
$ php app/console doctrine:schema:update --force
</syntaxhighlight>
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 [http://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html 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:<ref>[http://symfony.com/doc/current/book/doctrine.html#persisting-objects-to-the-database Persisting Objects to the Database], Doctrine documentation at Symphony documentation, "The Book"</ref>
<syntaxhighlight lang="php" highlight="17-18">
// 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());
}
}
</syntaxhighlight>
</syntaxhighlight>
== Notes ==
== Notes ==
<references/>
<references/>

Revision as of 21:27, 24 January 2015


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.

// 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:[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... */
}

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:[4]

// 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());
}

Notes

  1. Doctrine Documentation, Symfony Documentation, "The Book"
  2. Doctrine Property Mapping
  3. Doctrine Field Types Reference
  4. Persisting Objects to the Database, Doctrine documentation at Symphony documentation, "The Book"