Skip to content

Commit

Permalink
Unify common methods (#914)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored Nov 10, 2021
1 parent 0ec0229 commit f7805b6
Show file tree
Hide file tree
Showing 31 changed files with 204 additions and 317 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,10 @@ Resulting queries (I have removed back-ticks and parametric variables for readab
```sql
select id, name from client where name = "Pear Company" and is_deleted = 0;
insert into order (company_id, ref, delivery_date)
values (293, "TBL1", "2015-18-12");
values (293, "TBL1", "2015-18-12");
insert into order_lines (order_id, title, category_id, qty, price) values
(201, "Table", (select id from category where name = "furniture"), 2, 10.50),
(201, "Chair", (select id from category where name = "furniture"), 19, 3.25);
(201, "Table", (select id from category where name = "furniture"), 2, 10.50),
(201, "Chair", (select id from category where name = "furniture"), 19, 3.25);
```

If you have enjoyed those examples and would like to try them yourself, continue to https://github.com/atk4/data-primer.
Expand All @@ -350,14 +350,15 @@ Agile Data uses vendor-independent and lightweight `Model` class to describe you

``` php
class Client extends \Atk4\Data\Model {
public $table = 'client';
function init(): void {
parent::init();
public $table = 'client';

function init(): void {
parent::init();

$this->addFields(['name','address']);
$this->addFields(['name', 'address']);

$this->hasMany('Project', ['model' => [Project::class]]);
}
$this->hasMany('Project', ['model' => [Project::class]]);
}
}
```

Expand Down Expand Up @@ -496,7 +497,7 @@ If you wonder how those advanced features may impact performance of loading and


``` php
foreach($client->ref('Project') as $project) {
foreach ($client->ref('Project') as $project) {
echo $project->get('name')."\n"
}

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@
"ext-pdo": "*",
"atk4/core": "dev-develop",
"doctrine/dbal": "^2.13.3 || ^3.0",
"mvorisek/atk4-hintable": "~1.7.0"
"mvorisek/atk4-hintable": "~1.7.1"
},
"require-release": {
"php": ">=7.4 <8.2",
"ext-intl": "*",
"ext-pdo": "*",
"atk4/core": "~3.1.0",
"doctrine/dbal": "^2.13.3 || ^3.0",
"mvorisek/atk4-hintable": "~1.7.0"
"mvorisek/atk4-hintable": "~1.7.1"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.13",
Expand Down
16 changes: 8 additions & 8 deletions docs/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ would work without a change.

Another scenario which could benefit by type substitution would be::

foreach($accoutn->ref('Transactions') as $tr) {
foreach ($account->ref('Transactions') as $tr) {
echo get_class($tr)."\n";
}

Expand Down Expand Up @@ -107,9 +107,9 @@ of the record. Finally to help with performance, you can implement a switch::
Now, every time you iterate (or load) you can decide if you want to invoke type
substitution::

foreach($account->ref('Transactions', ['typeSubstitution' => true]) as $tr) {
foreach ($account->ref('Transactions', ['typeSubstitution' => true]) as $tr) {

$tr->verify(); // verify() method can be overloaded!
$tr->verify(); // verify() method can be overloaded!
}


Expand Down Expand Up @@ -521,7 +521,9 @@ Next we need to define reference. Inside Model_Invoice add::
$this->ref('InvoicePayment')->action('delete')->execute();

// If you have important per-row hooks in InvoicePayment
// $payment = $this->ref('InvoicePayment'); $payment->each(function () use ($payment) { $payment->delete(); });
// foreach ($this->ref('InvoicePayment') as $payment) {
// $payment->delete();
// }
});

You'll have to do a similar change inside Payment model. The code for '$j->'
Expand Down Expand Up @@ -811,12 +813,10 @@ field only to offer payments made by the same client. Inside Model_Invoice add::
In this case the payment_invoice_id will be set to ID of any payment by client
123. There also may be some better uses::

$cl->ref('Invoice')->each(function($m) {

foreach ($cl->ref('Invoice') as $m) {
$m->set('payment_invoice_id', $m->ref('payment_invoice_id')->tryLoadOne()->getId());
$m->save();

});
}

Narrowing Down Existing References
==================================
Expand Down
4 changes: 2 additions & 2 deletions docs/design.rst
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ Orders::
You can iterate over the DataSet::

$sum = 0;
foreach($db->add('Model_Order') as $order) {
foreach ($db->add('Model_Order') as $order) {
$sum += $order->get('amount');
}

Expand Down Expand Up @@ -417,7 +417,7 @@ The above is a Domain Model code. It will iterate through the DataSet of
a restriction::

$sum = 0;
foreach($db->add('Model_Order')->addCondition('is_paid', true) as $order) {
foreach ($db->add('Model_Order')->addCondition('is_paid', true) as $order) {
$sum += $order->get('amount');
}

Expand Down
5 changes: 2 additions & 3 deletions docs/model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ and even perform operations on multiple records (See `Persistence Actions` below

$m->action('delete')->execute(); // performs mass delete, hooks are not executed

$m->each(function () use ($m) { $m->delete(); }); // deletes each record, hooks are executed
foreach ($m as $entity) { $entity->delete(); } // deletes each record, hooks are executed

When data is loaded from associated Persistence, it is automatically converted into
a native PHP type (such as DateTime object) through a process called Typecasting. Various
Expand Down Expand Up @@ -208,8 +208,7 @@ Each model field is represented by a Field object::
Other persistence framework will use "properties", because individual objects may impact
performance. In ATK Data this is not an issue, because "Model" is re-usable::

foreach(new User($db) as $user) {

foreach (new User($db) as $user) {
// will be the same object every time!!
var_dump($user->getField['name']);

Expand Down
6 changes: 3 additions & 3 deletions docs/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ This code is specific to SQL databases, but can be used with any Model, so in
order to use grouping with Agile Data, your code would be::

$m = new \Atk4\Report\GroupModel(new Sale($db));
$m->groupBy(['contractor_to', 'type'], [ // groups by 2 columns
'c' => 'count(*)', // defines aggregate formulas for fields
'qty' => 'sum([])', // [] refers back to qty
$m->groupBy(['contractor_to', 'type'], [ // groups by 2 columns
'c' => 'count(*)', // defines aggregate formulas for fields
'qty' => 'sum([])', // [] refers back to qty
'total' => 'sum([amount])', // can specify any field here
]);

Expand Down
2 changes: 1 addition & 1 deletion docs/persistence/sql/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ or query::
When it's time to execute you can specify your PDO manually::

$rows = $expr->getRows($pdo);
foreach($rows as $row) {
foreach ($rows as $row) {
echo json_encode($row)."\n";
}

Expand Down
2 changes: 1 addition & 1 deletion docs/references.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ If you are worried about performance you can keep 2 models in memory::
$order = new Order($db);
$client = $order->refModel('client_id');

foreach($order as $o) {
foreach ($order as $o) {
$client = $client->load($o->get('client_id'));
}

Expand Down
18 changes: 9 additions & 9 deletions docs/results.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ Create your persistence object first then iterate it::
$db = \Atk4\Data\Persistence::connect($dsn);
$m = new Model_Client($db);

foreach($m as $id => $item) {
echo $id.": ".$item->get('name')."\n";
foreach ($m as $id => $item) {
echo $id . ': ' . $item->get('name') . "\n";
}

You must be aware that $item will actually be same as $m and will point to the model.
The model, however, will have the data loaded for you, so you can call methods for
each iteration like this::

foreach($m as $item) {
foreach ($m as $item) {
$item->sendReminder();
}

Expand All @@ -49,20 +49,20 @@ will consume memory), you can do it like this::

$cat = [];

foreach(new Model_Category($db) as $id => $c) {
foreach (new Model_Category($db) as $id => $c) {
$cat[$id] = clone $c;
}


Raw Data Fetching
----------------

.. php:method:: rawIterator()
.. php:method:: getRawIterator()
If you do not care about the hooks and simply wish to get the data, you can fetch
it::

foreach($m->rawIterator() as $row) {
foreach ($m->getRawIterator() as $row) {
var_dump($row); // array
}

Expand All @@ -82,17 +82,17 @@ Fetching data through action

You can invoke and iterate action (particularly SQL) to fetch the data::

foreach($m->action('select') as $row) {
foreach ($m->action('select') as $row) {
var_dump($row); // array
}

This has the identical behavior to $m->rawIterator();
This has the identical behavior to $m->getRawIterator();


Comparison of various ways of fetching
======================================

- getIterator - action(select), [ fetches row, set ID/Data, call afterLoad hook,
yields model ], unloads data
- rawIterator - action(select), [ fetches row, yields row ]
- getRawIterator - action(select), [ fetches row, yields row ]
- export - action(select), fetches all rows, returns all rows
2 changes: 1 addition & 1 deletion docs/sql.rst
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ Here is another example using PHP generator::
throw new Exception('Client must be loaded');
}

foreach($this->expr("call get_client_report_data([client_id, arg])", [
foreach ($this->expr("call get_client_report_data([client_id, arg])", [
'arg' => $arg,
'client_id' => $client_id,
]) as $row) {
Expand Down
2 changes: 1 addition & 1 deletion src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ public function __construct()
}
};
} else {
$this->getOwner()->checkPersistence();
$this->getOwner()->assertHasPersistence();
}
}

Expand Down
Loading

0 comments on commit f7805b6

Please sign in to comment.