1 Utilisation sans framework¶
1.1 Initialisation¶
Nous supposerons dans ce guide que vous avez déjà installé composer.
Les sources pour cet exemple se situerons dans src/VendorName/ApplicationName
il faut donc penser à bien ajouter
ce chemin dans l’autoload du composer.json :
{
"autoload": {
"psr-4": { "VendorName\\ApplicationName\\": "src/VendorName/ApplicationName"}
}
}
1.2 Installer Ting¶
Il suffit d’ajouter en tant que dépendance Ting à notre projet :
composer require ccmbenchmark/ting
1.3 Initialisation de la base de données¶
Pour ce guide on utilise world database qu’il faut installer dans une base de données MySQL. Vous pouvez vous référer au guide officiel pour savoir comment procéder.
1.4 Création des repository¶
La base de données world database
a trois tables composées ainsi :
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
Dans cet exemple nous ne prenons pas toutes les colonnes des tables pour alléger le guide
1.5 Création du repository City¶
<?php
namespace VendorName\ApplicationName\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(\VendorName\ApplicationName\Entity\City::class);
$metadata->setConnectionName('main');
$metadata->setDatabase('world');
$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;
}
}
1.6 Création du repository Country¶
<?php
namespace VendorName\ApplicationName\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(\VendorName\ApplicationName\Entity\Country::class);
$metadata->setConnectionName('main');
$metadata->setDatabase('world');
$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;
}
}
1.7 Création du repository CountryLanguage¶
<?php
namespace VendorName\ApplicationName\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(\VendorName\ApplicationName\Entity\Country::class);
$metadata->setConnectionName('main');
$metadata->setDatabase('world');
$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;
}
}
1.8 Configuration de la connexion à la base de données¶
<?php
namespace VendorName\ApplicationName;
require __DIR__ . '/../../../vendor/autoload.php';
$services = new \CCMBenchmark\Ting\Services();
$services->get('ConnectionPool')->setConfig([
'main' => [
'namespace' => '\CCMBenchmark\Ting\Driver\Mysqli',
'master' => [
'host' => 'localhost',
'user' => 'root',
'password' => '',
'port' => 3306,
]
]
]);
1.9 Configuration pour indiquer l’emplacement des repository¶
On configure l’emplacement des repository en complétant le fichier bootstrap.php
<?php
$services
->get('MetadataRepository')
->batchLoadMetadata('VendorName\ApplicationName\Repository', __DIR__ . '/Repository/*.php');
1.10 Création des entitées¶
Lorsque l’on a créé les repository, on a indiqué avec quelle entité il travaille : $metadata->setEntity(\VendorName\ApplicationName\Entity\Country::class);
On va donc maintenant créer une entité pour chaque repository.
<?php
namespace VendorName\ApplicationName\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 VendorName\ApplicationName\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 VendorName\ApplicationName\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;
}
}
On a maintenant les repository et les entitées correspondantes, on va pouvoir faire des requêtes.
1.11 Vous pouvez maintenant utiliser Ting¶
$debug = new \CCMBenchmark\Ting\Util\Debug();
$cityRepository = $services->get('RepositoryFactory')
->get(\VendorName\ApplicationName\Repository\City::class);
$debug->dump($cityRepository->get(3));