Doctrine
How to use databases via Doctrine in kehikko.
Supports:
This document only describes configuration and best practices of using Doctrine with kehikko. At least some basic knowledge of SQL and/or MongoDB and Doctrine is required.
SQL
Using SQL via Doctrine Entity Manager.
Metadata for entities must be written in YAML files that end in .dcm.yml
.
Installation
sudo apt install php-mysql
composer require kehikko/data
composer require doctrine/orm
Configuration
Doctrine configuration should be set in config/config-local.yaml
(not config/config.yaml
) somewhat like this:
doctrine:
sql:
driver: pdo_mysql
host: localhost
port: 3306
dbname: sql_db_name
user: username
password: pass
See Doctrine Installation and Configuration for more.
Command-Line
File config/cli-config.php
creates Doctrine Entity Manager for doctrine
command.
There is no need to modify this file.
After configuration is ok and Doctrine packages have been installed,
try running ./doctrine
command from project root. It should output list of commands
available:
aehparta@dev:~/www/kehikko$ ./doctrine
Doctrine Command Line Interface version 2.4.8
Usage:
command [options] [arguments]
Options:
-h, --help Display this help message
...
Usage
Kernel will search SQL entity metadata under directory named sql
from all module parent directories listed in configuration modules
section.
Metadata entities must be written in YAML and filenames must end in .dcm.yml
.
Example of entity metadata file placement in filesystem:
modules/
MyModule/
MyEntity.php
sql/
MyEntity.dcm.yml
It is recommended to write simple entity classes without extending them example
with \Core\Module
so it is easier to transfer them to other systems if needed.
That said:
- If entity class needs
kernel
, it should be requested usingkernel::getInstance()
- If entity class needs Entity Manager, it should be requested using global function
em()
Try to avoid doing either.
Code Examples
class TestController extends Controller
{
public function testAction()
{
$repository = em()->getRepository('MyEntity');
$my_entities = $repository->findAll();
...
$my_entity = new MyEntity();
em()->persist($my_entity);
em()->flush();
...
}
}
class MyClass extends \Core\Module
{
public function doSomething()
{
$repository = em()->getRepository('MyEntity');
$my_entities = $repository->findAll();
...
$my_entity = new MyEntity();
em()->persist($my_entity);
em()->flush();
...
}
}
MongoDB
Using MongoDB via Doctrine Document Manager.
Metadata for entities must be written in YAML files that end in .dcml.yml
.
Installation
sudo apt install php-mongodb
composer require kehikko/data
composer require doctrine/mongodb
Configuration
Doctrine configuration should be set in config/config-local.yaml
(not config/config.yaml
) somewhat like this:
doctrine:
mongo:
# localhost:27017 is default so in that case this line is not needed
server: mongodb://localhost:27017
# this should be set, though it defaults to 'framework'
dbname: mongo_db_name
Usage
Kernel will search MongoDB entity metadata under directory named mongodb
from all module parent directories listed in configuration modules
section.
Metadata entities must be written in YAML and filenames must end in .dcm.yml
.
Example of entity metadata file placement in filesystem:
modules/
MyModule/
MyEntity.php
mongodb/
MyEntity.dcm.yml
Otherwise using MongoDB through Doctrine is pretty much the same as using SQL.
Code Examples
class TestController extends Controller
{
public function testAction()
{
$repository = dm()->getRepository('MyEntity');
$my_entities = $repository->findAll();
...
$my_entity = new MyEntity();
dm()->persist($my_entity);
dm()->flush();
...
}
}
class MyClass extends \Core\Module
{
public function doSomething()
{
$repository = dm()->getRepository('MyEntity');
$my_entities = $repository->findAll();
...
$my_entity = new MyEntity();
dm()->persist($my_entity);
dm()->flush();
...
}
}