Пример #1
0
    public function __construct(string $product, string $price)
    {
        $this->product = $product;
        $this->price = $price;
    }
    public function getProduct() : string
    {
        return $this->product;
    }
    public function getPrice() : \Currency
    {
        return $this->price;
    }
}
// we need a custom TypeMapper for our value object
class CurrencyTypeMapper implements \Agares\MicroORM\TypeMapperInterface
{
    public function fromString(string $fieldName, array $fields)
    {
        return new Currency($fields[$fieldName], $fields[$fieldName . '_currency']);
    }
}
// let's try this:
$typeMappers = ['string' => new \Agares\MicroORM\TypeMappers\StringTypeMapper(), 'int' => new \Agares\MicroORM\TypeMappers\IntegerTypeMapper(), 'Currency' => new CurrencyTypeMapper()];
$entityMapper = new \Agares\MicroORM\EntityMapper($typeMappers);
$queryAdapter = new \Agares\MicroORM\QueryAdapter($databaseAdapter, $entityMapper);
$transactions = $queryAdapter->executeQuery('SELECT product, price, price_currency FROM transactions', $entityDefinitionCreator->create(Transaction::class));
foreach ($transactions as $transaction) {
    /** @var Transaction $transaction */
    printf('%s (%s)%s', $transaction->getProduct(), (string) $transaction->getPrice(), PHP_EOL);
}
Пример #2
0
     *
     * @param string $firstname
     * @param string $lastname
     * @param int $age
     */
    public function __construct(string $firstname, string $lastname, int $age)
    {
        $this->firstname = $firstname;
        $this->lastname = $lastname;
        $this->age = $age;
    }
    public function getFirstname() : string
    {
        return $this->firstname;
    }
    public function getLastname() : string
    {
        return $this->lastname;
    }
    public function getAge() : int
    {
        return $this->age;
    }
}
// And now we can query the DB
// The types of properties will be inferred from getters
$people = $queryAdapter->executeQuery('SELECT firstname, lastname, age FROM people', $entityDefinitionCreator->create(Person::class));
foreach ($people as $person) {
    /** @var Person $person */
    printf('%s %s (age %d)%s', $person->getFirstname(), $person->getLastname(), $person->getAge(), PHP_EOL);
}