Esempio n. 1
0
<?php

declare (strict_types=1);
require_once __DIR__ . '/../vendor/autoload.php';
// initialisation
$pdo = new \PDO('sqlite::memory:');
$databaseAdapter = new \Agares\MicroORM\PDODbAdapter($pdo);
$entityDefinitionCreator = new \Agares\MicroORM\EntityDefinitionCreator(new \Agares\MicroORM\FieldNameMappers\StripGet());
// create a test table
$databaseAdapter->executeCommand('CREATE TABLE transactions (product TEXT, price TEXT, price_currency TEXT)');
// create some test data
$transactions = [['Wonderful Tea', '9.99', 'GBP'], ['Lousy Coffee', '10', 'CZK']];
foreach ($transactions as $transaction) {
    $parameters = array(':product' => $transaction[0], ':price' => $transaction[1], ':price_currency' => $transaction[2]);
    $databaseAdapter->executeCommand('INSERT INTO transactions VALUES(:product, :price, :price_currency)', $parameters);
}
// define a value object
class Currency
{
    private $amount = '0';
    private $currency = 'XXX';
    public function __construct(string $amount, string $currency)
    {
        $this->amount = $amount;
        $this->currency = $currency;
    }
    public function getAmount() : string
    {
        return $this->amount;
    }
    public function getCurrency() : string
Esempio n. 2
0
<?php

declare (strict_types=1);
require_once __DIR__ . '/../vendor/autoload.php';
// this initialisation might look complex, but remember that it's done only once - in your DI container
$pdo = new \PDO('sqlite::memory:');
$databaseAdapter = new \Agares\MicroORM\PDODbAdapter($pdo);
$entityMapper = new \Agares\MicroORM\EntityMapper();
$queryAdapter = new \Agares\MicroORM\QueryAdapter($databaseAdapter, $entityMapper);
$entityDefinitionCreator = new \Agares\MicroORM\EntityDefinitionCreator(new \Agares\MicroORM\FieldNameMappers\ToUnderscores());
// let's create some data!
$queryAdapter->executeCommand('CREATE TABLE people (firstname TEXT, lastname TEXT, age INT)');
$people = [['Jeff', 'Lebowski', 30], ['Bunny', 'Lebowski', 25], ['The', 'Dude', 50]];
foreach ($people as $person) {
    $parameters = array(':firstname' => $person[0], ':lastname' => $person[1], ':age' => $person[2]);
    $queryAdapter->executeCommand('INSERT INTO people VALUES(:firstname, :lastname, :age)', $parameters);
}
// time to start the real fun!
// Let's create an entity
class Person
{
    /**
     * @var string
     */
    private $firstname;
    /**
     * @var string
     */
    private $lastname;
    /**
     * @var int