1
|
|
|
<?php
|
|
|
|
|
2
|
|
|
declare(strict_types = 1);
|
3
|
|
|
|
4
|
|
|
require_once __DIR__.'/../vendor/autoload.php';
|
5
|
|
|
|
6
|
|
|
// this initialisation might look complex, but remember that it's done only once - in your DI container
|
7
|
|
|
$pdo = new \PDO('sqlite::memory:');
|
8
|
|
|
$databaseAdapter = new \Agares\MicroORM\PDODbAdapter($pdo);
|
9
|
|
|
$entityMapper = new \Agares\MicroORM\EntityMapper();
|
10
|
|
|
$queryAdapter = new \Agares\MicroORM\QueryAdapter($databaseAdapter, $entityMapper);
|
11
|
|
|
$entityDefinitionCreator = new \Agares\MicroORM\EntityDefinitionCreator(new \Agares\MicroORM\FieldNameMappers\ToUnderscores());
|
12
|
|
|
|
13
|
|
|
// let's create some data!
|
14
|
|
|
$queryAdapter->executeCommand('CREATE TABLE people (firstname TEXT, lastname TEXT, age INT)');
|
15
|
|
|
$people = [
|
16
|
|
|
['Jeff', 'Lebowski', 30],
|
17
|
|
|
['Bunny', 'Lebowski', 25],
|
18
|
|
|
['The', 'Dude', 50]
|
19
|
|
|
];
|
20
|
|
|
|
21
|
|
|
foreach ($people as $person) {
|
22
|
|
|
$parameters = array(
|
23
|
|
|
':firstname' => $person[0],
|
24
|
|
|
':lastname' => $person[1],
|
25
|
|
|
':age' => $person[2]
|
26
|
|
|
);
|
27
|
|
|
$queryAdapter->executeCommand('INSERT INTO people VALUES(:firstname, :lastname, :age)', $parameters);
|
28
|
|
|
}
|
29
|
|
|
|
30
|
|
|
// time to start the real fun!
|
31
|
|
|
// Let's create an entity
|
32
|
|
|
class Person
|
|
|
|
|
33
|
|
|
{
|
34
|
|
|
/**
|
35
|
|
|
* @var string
|
36
|
|
|
*/
|
37
|
|
|
private $firstname;
|
38
|
|
|
|
39
|
|
|
/**
|
40
|
|
|
* @var string
|
41
|
|
|
*/
|
42
|
|
|
private $lastname;
|
43
|
|
|
|
44
|
|
|
/**
|
45
|
|
|
* @var int
|
46
|
|
|
*/
|
47
|
|
|
private $age;
|
48
|
|
|
|
49
|
|
|
/**
|
50
|
|
|
* This constructor will not be called by MicroORM.
|
51
|
|
|
*
|
52
|
|
|
* @param string $firstname
|
53
|
|
|
* @param string $lastname
|
54
|
|
|
* @param int $age
|
55
|
|
|
*/
|
56
|
|
|
public function __construct(string $firstname, string $lastname, int $age)
|
57
|
|
|
{
|
58
|
|
|
$this->firstname = $firstname;
|
59
|
|
|
$this->lastname = $lastname;
|
60
|
|
|
$this->age = $age;
|
61
|
|
|
}
|
62
|
|
|
|
63
|
|
|
public function getFirstname() : string
|
64
|
|
|
{
|
65
|
|
|
return $this->firstname;
|
66
|
|
|
}
|
67
|
|
|
|
68
|
|
|
public function getLastname() : string
|
69
|
|
|
{
|
70
|
|
|
return $this->lastname;
|
71
|
|
|
}
|
72
|
|
|
|
73
|
|
|
public function getAge() : int
|
74
|
|
|
{
|
75
|
|
|
return $this->age;
|
76
|
|
|
}
|
77
|
|
|
}
|
78
|
|
|
|
79
|
|
|
// And now we can query the DB
|
80
|
|
|
// The types of properties will be inferred from getters
|
81
|
|
|
$people = $queryAdapter->executeQuery('SELECT firstname, lastname, age FROM people', $entityDefinitionCreator->create(Person::class));
|
82
|
|
|
|
83
|
|
|
foreach ($people as $person) {
|
84
|
|
|
/** @var Person $person */
|
85
|
|
|
printf('%s %s (age %d)%s', $person->getFirstname(), $person->getLastname(), $person->getAge(), PHP_EOL);
|
86
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.