2 Getting started with Symfony 2.8¶
2.1 Setup¶
There are some prerequesites, you should have Symfony installer and composer. Create a new project : symfony new world_site 2.8
2.2 Delete Doctrine dependencies¶
Ting replaces Doctrine, so we should remove Doctrine.
Remove the line new Doctrine\Bundle\DoctrineBundle\DoctrineBundle()
in file app/AppKernel.php
In file app/config/config.yml
remove the Doctrine block :
# Doctrine Configuration
doctrine:
dbal:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
# path: "%database_path%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
Remove Doctrine :
composer remove doctrine/orm
composer remove doctrine/doctrine-bundle
2.3 Install Ting¶
Ting require PHP 5.5, you need to modify composer.json
and replace :
{
"platform": {
"php": "5.3.9"
}
}
with
{
"platform": {
"php": "5.5"
}
}
Install Ting Bundle: composer require ccmbenchmark/ting_bundle
Don’t forget to register bundles in app/AppKernel.php
:
new Doctrine\Bundle\DoctrineCacheBundle\DoctrineCacheBundle(),
new CCMBenchmark\TingBundle\TingBundle(),
2.4 Setup database¶
For this tutorial we are using world database which you need to install into a MySQL. You can refer to the official guide.
2.5 Creating repositories¶
world database
is composed of three tables :
City | |
---|---|
ID | int(11) |
Name | char(35) |
CountryCode | char(3) |
Population | int(11) |
Country | |
---|---|
Code | int(3) |
Name | char(52) |
Region | char(26) |
Population | int(11) |
CountryLanguage | |
---|---|
CountryCode | char(3) |
Language | char(30) |
Percentage | float(4, 1) |
Note
In this tutorial we don’t take all columns of tables
2.6 Creating City repository¶
<?php
namespace AppBundle\Repository;
use CCMBenchmark\Ting\Exception;
use CCMBenchmark\Ting\Repository\Metadata;
use CCMBenchmark\Ting\Repository\MetadataInitializer;
use CCMBenchmark\Ting\Repository\Repository;
use CCMBenchmark\Ting\Serializer\SerializerFactoryInterface;
class City extends Repository implements MetadataInitializer
{
/**
* @param SerializerFactoryInterface $serializerFactory
*
* @return Metadata
* @throws Exception
*/
public static function initMetadata(SerializerFactoryInterface $serializerFactory, array $options = [])
{
$metadata = new Metadata($serializerFactory);
$metadata->setEntity(\AppBundle\Entity\City::class);
$metadata->setConnectionName($options['connection']);
$metadata->setDatabase($options['database']);
$metadata->setTable('City');
$metadata
->addField([
'primary' => true,
'autoincrement' => true,
'fieldName' => 'id',
'columnName' => 'ID',
'type' => 'int'
])
->addField([
'fieldName' => 'name',
'columnName' => 'Name',
'type' => 'string'
])
->addField([
'fieldName' => 'countryCode',
'columnName' => 'CountryCode',
'type' => 'string'
])
->addField([
'fieldName' => 'population',
'columnName' => 'Population',
'type' => 'int'
]);
return $metadata;
}
}
2.7 Creating Country repository¶
<?php
namespace AppBundle\Repository;
use CCMBenchmark\Ting\Exception;
use CCMBenchmark\Ting\Repository\Metadata;
use CCMBenchmark\Ting\Repository\MetadataInitializer;
use CCMBenchmark\Ting\Repository\Repository;
use CCMBenchmark\Ting\Serializer\SerializerFactoryInterface;
class Country extends Repository implements MetadataInitializer
{
/**
* @param SerializerFactoryInterface $serializerFactory
*
* @return Metadata
* @throws Exception
*/
public static function initMetadata(SerializerFactoryInterface $serializerFactory, array $options = [])
{
$metadata = new Metadata($serializerFactory);
$metadata->setEntity(\AppBundle\Entity\Country::class);
$metadata->setConnectionName($options['connection']);
$metadata->setDatabase($options['database']);
$metadata->setTable('Country');
$metadata
->addField([
'primary' => true,
'fieldName' => 'code',
'columnName' => 'Code',
'type' => 'string'
])
->addField([
'fieldName' => 'name',
'columnName' => 'Name',
'type' => 'string'
])
->addField([
'fieldName' => 'region',
'columnName' => 'Region',
'type' => 'string'
])
->addField([
'fieldName' => 'population',
'columnName' => 'Population',
'type' => 'int'
]);
return $metadata;
}
}
2.8 Creating CountryLanguage repository¶
<?php
namespace AppBundle\Repository;
use CCMBenchmark\Ting\Exception;
use CCMBenchmark\Ting\Repository\Metadata;
use CCMBenchmark\Ting\Repository\MetadataInitializer;
use CCMBenchmark\Ting\Repository\Repository;
use CCMBenchmark\Ting\Serializer\SerializerFactoryInterface;
class CountryLanguage extends Repository implements MetadataInitializer
{
/**
* @param SerializerFactoryInterface $serializerFactory
*
* @return Metadata
* @throws Exception
*/
public static function initMetadata(SerializerFactoryInterface $serializerFactory, array $options = [])
{
$metadata = new Metadata($serializerFactory);
$metadata->setEntity(\AppBundle\Entity\Country::class);
$metadata->setConnectionName($options['connection']);
$metadata->setDatabase($options['database']);
$metadata->setTable('CountryLanguage');
$metadata
->addField([
'fieldName' => 'countryCode',
'columnName' => 'CountryCode',
'type' => 'string'
])
->addField([
'fieldName' => 'language',
'columnName' => 'Language',
'type' => 'string'
])
->addField([
'fieldName' => 'percentage',
'columnName' => 'Percentage',
'type' => 'double'
]);
return $metadata;
}
}
2.9 Database connection settings¶
ting:
connections:
main:
namespace: CCMBenchmark\Ting\Driver\Mysqli
master:
host: localhost
user: root
password: ""
port: 3306
Note
We named the connection main
we going to used it below.
2.10 Repositories settings¶
We configure the location and connection of repositories.
ting:
repositories:
World:
namespace: AppBundle\Repository
directory: "@AppBundle/Repository"
glob: "*.php"
options:
default:
connection: main
database: world
2.11 Creating entities¶
Previously we created the repository and linked each one with one entity : $metadata->setEntity(\VendorName\ApplicationName\Entity\Country::class);
Now we create entity for each repository.
<?php
namespace AppBundle\Entity;
use CCMBenchmark\Ting\Entity\NotifyProperty;
use CCMBenchmark\Ting\Entity\NotifyPropertyInterface;
class City implements NotifyPropertyInterface
{
use NotifyProperty;
private $id;
private $name = '';
private $countryCode = '';
private $population = 0;
/**
* @param int $id
*/
public function setId($id)
{
$this->propertyChanged('id', $this->id, (int) $id);
$this->id = (int) $id;
}
/**
* @return int|null
*/
public function getId()
{
return $this->id;
}
/**
* @param string $name
*/
public function setName($name)
{
$this->propertyChanged('name', $this->name, (string) $name);
$this->name = (string) $name;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $countryCode
*/
public function setCountryCode($countryCode)
{
$this->propertyChanged('countryCode', $this->countryCode, (string) $countryCode);
$this->countryCode = (string) $countryCode;
}
/**
* @return string
*/
public function getCountryCode()
{
return $this->countryCode;
}
/**
* @param int $population
*/
public function setPopulation($population)
{
$this->propertyChanged('population', $this->population, (int) $population);
$this->population = (int) $population;
}
/**
* @return int
*/
public function getPopulation()
{
return $this->population;
}
}
<?php
namespace AppBundle\Entity;
use CCMBenchmark\Ting\Entity\NotifyProperty;
use CCMBenchmark\Ting\Entity\NotifyPropertyInterface;
class Country implements NotifyPropertyInterface
{
use NotifyProperty;
private $code = '';
private $name = '';
private $region = '';
private $population = 0;
/**
* @param string $code
*/
public function setCode($code)
{
$this->propertyChanged('code', $this->code, (string) $code);
$this->code = (string) $code;
}
/**
* @return string
*/
public function getCode()
{
return $this->code;
}
/**
* @param string $name
*/
public function setName($name)
{
$this->propertyChanged('name', $this->name, (string) $name);
$this->name = (string) $name;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $region
*/
public function setRegion($region)
{
$this->propertyChanged('region', $this->region, (string) $region);
$this->region = (string) $region;
}
/**
* @return string
*/
public function getRegion()
{
return $this->region;
}
/**
* @param int $population
*/
public function setPopulation($population)
{
$this->propertyChanged('population', $this->population, (int) $population);
$this->population = (int) $population;
}
/**
* @return int
*/
public function getPopulation()
{
return $this->population;
}
}
<?php
namespace AppBundle\Entity;
use CCMBenchmark\Ting\Entity\NotifyProperty;
use CCMBenchmark\Ting\Entity\NotifyPropertyInterface;
class CountryLanguage implements NotifyPropertyInterface
{
use NotifyProperty;
private $countryCode = '';
private $language = '';
private $percentage = 0.0;
/**
* @param string $countryCode
*/
public function setCountryCode($countryCode)
{
$this->propertyChanged('countryCode', $this->countryCode, (string) $countryCode);
$this->countryCode = (string) $countryCode;
}
/**
* @return string
*/
public function getCountryCode()
{
return $this->countryCode;
}
/**
* @param string $language
*/
public function setLanguage($language)
{
$this->propertyChanged('language', $this->language, (string) $language);
$this->language = (string) $language;
}
/**
* @return string
*/
public function getLanguage()
{
return $this->language;
}
/**
* @param double $percentage
*/
public function setPercentage($percentage)
{
$this->propertyChanged('percentage', $this->percentage, (double) $percentage);
$this->percentage = (double) $percentage;
}
/**
* @return double
*/
public function getPercentage()
{
return $this->percentage;
}
}
Now repositories and entities are configured we can do queries.
2.12 You can now use Ting¶
In DefaultController::indexAction
:
$debug = new \CCMBenchmark\Ting\Util\Debug();
$city = $this->get('ting')
->get(\AppBundle\Repository\City::class)->get(['name' => 'Paris']);
$debug->dump($city);