Skip to content

Instantly share code, notes, and snippets.

@nimmneun
Created October 14, 2015 06:18
Show Gist options
  • Save nimmneun/cc323762032dc73119ff to your computer and use it in GitHub Desktop.
Save nimmneun/cc323762032dc73119ff to your computer and use it in GitHub Desktop.
Oversimplified Order Aggregate ... just trying stuff *lol
<?php
/**
* @inherits ArrayAccess, IteratorAggregate, Countable from Collection
* @author neun
* @since 12.10.2015 22:24
*/
class OrderAggregate extends Collection
{
/**
* @var Order
*/
protected $order;
/**
* @var Customer
*/
protected $customer;
/**
* @var OrderItem[]
*/
protected $items;
public function __construct($orderId)
{
$sql = "SELECT * FROM `order` WHERE id = {$orderId}";
$this->order = pdo('mono')->query($sql)->fetchObject('Order');
$sql = "SELECT * FROM `customer` WHERE id = {$this->order->customerId()}";
$this->customer = pdo('mono')->query($sql)->fetchObject('Customer');
$sql = "SELECT * FROM `order_item` WHERE order_id = {$orderId}";
$stm = pdo('mono')->query($sql);
while ($item = $stm->fetchObject('OrderItem')) {
$this->add($item);
}
}
public function order()
{
return $this->order;
}
public function customer()
{
return $this->customer;
}
}
@nimmneun
Copy link
Author

Well ... I guess I just learned that ...

PDOStatement->fetchObject('Object')

... actually not only fills the properties of a model ... but also retains their visibility if column_name equals property_name ... and since you dont access properties directly your getters can still look like...

public function orderNumber() { return $this->order_number; }

sw33t =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment