Skip to content

Instantly share code, notes, and snippets.

@bizstation
Last active June 12, 2017 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bizstation/b6b4ff5be2b6e0d4209f52ac198a766b to your computer and use it in GitHub Desktop.
Save bizstation/b6b4ff5be2b6e0d4209f52ac198a766b to your computer and use it in GitHub Desktop.
Example with Transactd PHP ORM
<?php
require_once(__DIR__ . "/vendor/autoload.php");
use BizStation\Transactd\Transactd;
use BizStation\Transactd\Database;
use BizStation\Transactd\Tabledef;
use BizStation\Transactd\Benchmark;
use BizStation\Transactd\Nstable;
use Transactd\QueryExecuter;
use Transactd\IOException;
use Transactd\Model;
use Transactd\Collection;
use Transactd\JsonSerializable;
class_alias('Transactd\DatabaseManager', 'DB');
CONST masterUri = 'tdap://localhost/salesdb';
CONST slaveUri = 'tdap://localhost/salesdb';
class Customer extends Model
{
protected static $guarded = ['id'];
public $id = 0;
public function invoices()
{
return $this->hasMany('Invoice');
}
public function transactions($startDate, $endDate)
{
return Invoice::index(1)->keyValue($this->id, $startDate)
->where('customer_id', $this->id)->where('date', '<=', $endDate)->get();
}
}
class Product extends Model
{
protected static $guarded = ['id'];
public function stock()
{
return $this->hasOne('Stock', 0, 'code');
}
}
class Stock extends Model
{
protected static $guarded = [];
public function product()
{
return $this->belongsTo('Product', 'code', 1);
}
}
Trait AmountTrait
{
public $sales = 0;
public $tax = 0;
public $payment = 0;
public $balance = 0;
public function reset()
{
$this->sales = 0;
$this->tax = 0;
$this->payment = 0;
}
public function sum($amount)
{
$this->sales += $amount->sales;
$this->tax += $amount->tax;
$this->payment += $amount->payment;
}
public function total()
{
return $this->sales + $this->tax - $this->payment;
}
}
class InvoiceAmount
{
use AmountTrait;
use JsonSerializable;
public function __construct()
{
$this->className = get_class($this);
}
}
class Invoice extends Model
{
public static $aliases = ['sales_amount' => 'sales', 'tax_amount' => 'tax', 'payment_amount' => 'payment', 'balance_amount' => 'balance'];
public static $transferMap = ['sales' => 'amount', 'tax' => 'amount', 'payment' => 'amount', 'balance' => 'amount'];
public static $guarded = ['id'];
public $id = 0;
public $amount = null;
public function __construct()
{
parent::__construct();
$this->amount = new InvoiceAmount;
}
public function items()
{
return $this->hasMany('InvoiceItem');
}
public function customer()
{
return $this->belongsTo('Customer');
}
}
class InvoiceItem extends Model
{
protected static $table = 'invoice_items';
protected static $guarded = [];
public function invoice()
{
return $this->belongsTo('Invoice');
}
public function stock()
{
return $this->hasOne('Stock', 0, 'product_code');
}
public function product()
{
return $this->hasOne('Product', 1, 'product_code');
}
}
class DailySummary extends Model
{
use AmountTrait;
protected static $guarded = [];
protected static $table = 'daily_summaries';
static protected $aliases = ['slaes_amount' => 'sales', 'tax_amount' => 'tax', 'payment_amount' => 'payment'];
public function invoices()
{
return $this->hasMany('Invoice', 2, 'date');
}
}
// Please `connect` at the beginning of the application.
// DB::connect(URI, URI);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment