1. Go to this page and download the library: Download atk4/data library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
atk4 / data example snippets
$vipClientModel = (new Client($db))->addCondition('is_vip', true);
// express total for all VIP client invoices. The value of the variable is an object
$totalDueModel = $vipClientModel->ref('Invoice')->action('fx', ['sum', 'total']);
// single database query is executed here, but not before!
echo $totalDueModel->getOne();
$grid = new \Atk4\Ui\Table();
$data = new Order($db);
$data->addCondition('is_new', true);
$data->addCondition('client_id', $_GET['client_id']);
$grid->setModel($data);
$html = $grid->render();
class JobReport extends Job
{
protected function init(): void
{
parent::init();
// Invoice contains Lines that may relevant to this job
$invoice = new Invoice($this->getPersistence());
// we need to ignore draft invoices
$invoice->addCondition('status', '!=', 'draft');
// build relation between job and invoice line
$this->hasMany('InvoiceLines', ['model' => static function () use ($invoice) {
return $invoice->ref('Lines');
}])
->addField('invoiced', [
'aggregate' => 'sum',
'field' => 'total',
'type' => 'atk4_money'
]);
// build relation between Job and Timesheets
$this->hasMany('Timesheets', ['model' => static function (Persistence $persistence) {
// next we need to see how much is reported through timesheets
$timesheet = new Timesheet($persistence);
// timesheet relates to client, import client.hourly_rate as expression
$timesheet->getReference('client_id')->addField('hourly_rate');
// calculate timesheet cost expression
$timesheet->addExpression('cost', ['expr' => '[hours] * [hourly_rate]']);
return $timesheet;
}])
->addField('reported', [
'aggregate' => 'sum',
'field' => 'cost',
'type' => 'atk4_money'
]);
// finally lets calculate profit
$this->addExpression('profit', ['expr' => '[invoiced] - [reported]']);
// profit margin could be also useful
$this->addExpression('profit_margin', ['expr' => 'coalesce([profit] / [invoiced], 0)']);
}
}
$grid = new \Atk4\Ui\Grid();
$data = new JobReport($db);
$grid->setModel($data);
$html = $grid->render();
$chart = new \Atk4\Chart\BarChart();
$data = new JobReport($db);
// BarChart wants aggregated data
$data->addExpression('month', ['expr' => 'month([date])']);
$aggregate = new AggregateModel($data);
$aggregate->setGroupBy(['month'], [
'profit_margin' => ['expr' => 'sum'],
]);
// associate presentation with data
$chart->setModel($aggregate, ['month', 'profit_margin']);
$html = $chart->html();
$m = new Client($db);
echo $m->addCondition('vip', true)
->ref('Order')->ref('Line')->action('fx', ['sum', 'total'])->getOne();
class Client extends \Atk4\Data\Model
{
public $table = 'client';
protected function init(): void
{
parent::init();
$this->addField('name');
$this->addField('address');
$this->hasMany('Project', ['model' => [Project::class]]);
}
}
foreach ($client->ref('Project') as $project) {
echo $project->get('name') . "\n";
}
// $project refers to same object at all times, but $project's active data
// is re-populated on each iteration
namespace my;
class User extends \Atk4\Data\Model
{
public $table = 'user';
protected function init(): void
{
parent::init();
$this->addField('email');
$this->addField('name');
$this->addField('password');
// add your table fields here
}
}