Skip to content

Commit

Permalink
Minor tests and CS refactoring (#1050)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Aug 15, 2022
1 parent 86ee209 commit c6bfad1
Show file tree
Hide file tree
Showing 39 changed files with 482 additions and 482 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ class Client extends \Atk4\Data\Model {
function init(): void {
parent::init();

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

$this->hasMany('Project', ['model' => [Project::class]]);
}
Expand Down Expand Up @@ -583,7 +584,9 @@ class User extends \Atk4\Data\Model
{
parent::init();

$this->addFields(['email', 'name', 'password']);
$this->addField('email');
$this->addField('name');
$this->addField('password');

// add your table fields here
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
},
"require-release": {
"php": ">=7.4 <8.3",
"atk4/core": "~3.2.0",
"atk4/core": "^4.0",
"doctrine/dbal": "~3.3.0",
"mvorisek/atk4-hintable": "~1.9.0"
},
Expand Down
8 changes: 0 additions & 8 deletions docs/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ 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) {

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

Expand All @@ -133,13 +132,11 @@ I will be looking to create the following fields:
To implement the above, I'll create a new class::

class Controller_Audit {

use \Atk4\Core\InitializerTrait {
init as private _init;
}
use \Atk4\Core\TrackableTrait;
use \Atk4\Core\AppScopeTrait;

}

TrackableTrait means that I'll be able to add this object inside model with
Expand Down Expand Up @@ -214,7 +211,6 @@ soft-delete controller for Agile Data (for educational purposes).
Start by creating a class::

class Controller_SoftDelete {

use \Atk4\Core\InitializerTrait {
init as private _init;
}
Expand Down Expand Up @@ -325,7 +321,6 @@ achieved through a pretty simple controller. In fact I'm reusing the one from
before and just slightly modifying it::

class Controller_SoftDelete {

use \Atk4\Core\InitializerTrait {
init as private _init;
}
Expand Down Expand Up @@ -570,12 +565,10 @@ payment towards a most suitable invoice::
$invoices->setOrder('date');

while($this->get('amount_due') > 0) {

// see if any invoices match by 'reference'
$invoice = $invoices->tryLoadBy('reference', $this->get('reference'));

if ($invoice === null) {

// otherwise load any unpaid invoice
$invoice = $invoices->tryLoadAny();

Expand Down Expand Up @@ -612,7 +605,6 @@ category_id::

class Model_Invoice extends \Atk4\Data\Model {
function init(): void {

parent::init();

...
Expand Down
4 changes: 1 addition & 3 deletions docs/design.rst
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ Code::

class Model_Client extends Model_User {
public function sendPasswordReminder() {

mail($this->get('email'), 'Your password is: ' . $this->get('password'));
}
}
Expand Down Expand Up @@ -336,8 +335,7 @@ Hooks can help you perform operations when object is being persisted::
protected function init(): void {
parent::init();

// addField() declaration
// addExpression('is_password_expired')
// add fields here

$this->onHookShort(Model::HOOK_BEFORE_SAVE, function () {
if ($this->isDirty('password')) {
Expand Down
11 changes: 7 additions & 4 deletions docs/expressions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ corresponds to an expression:
Example will calculate "total_gross" by adding up values for "net" and "vat"::

$m = new Model_Invoice($db);
$m->addFields(['total_net', 'total_vat']);

$m->addField('total_net');
$m->addField('total_vat');
$m->addExpression('total_gross', ['expr' => '[total_net] + [total_vat]']);

$m = $m->load(1);

echo $m->get('total_gross');
Expand Down Expand Up @@ -121,7 +122,8 @@ the following model::
{
parent::init();

$this->addFields(['a', 'b']);
$this->addField('a');
$this->addField('b');

$this->addExpression('sum', ['expr' => '[a] + [b]']);
}
Expand Down Expand Up @@ -169,7 +171,8 @@ Then try the following code::
{
parent::init();

$this->addFields(['a', 'b']);
$this->addField('a');
$this->addField('b');
}
}

Expand Down
13 changes: 2 additions & 11 deletions docs/model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,9 @@ Persistence object. It is commonly used to declare fields, conditions, relations
You may safely rely on `$this->getPersistence()` result to make choices::

if ($this->getPersistence() instanceof \Atk4\Data\Persistence\Sql) {

// Calculating on SQL server is more efficient!!
$this->addExpression('total', ['expr' => '[amount] + [vat]']);
} else {

// Fallback
$this->addCalculatedField('total', ['expr' => function ($m) {
return $m->get('amount') + $m->get('vat');
Expand All @@ -181,11 +179,9 @@ you use Persistence's "afterAdd" hook. This will not affect ALL models but just
which are associated with said persistence::

$db->onHook(Persistence::HOOK_AFTER_ADD, function ($p, $m) use ($acl) {

$fields = $m->getFields();

$acl->disableRestrictedFields($fields);

});

$invoice = new Invoice($db);
Expand All @@ -206,8 +202,7 @@ performance. In ATK Data this is not an issue, because "Model" is re-usable::
var_dump($user->getField['name']);

// this is also the same object every time!!
var_dump($user)

var_dump($user);
}

Instead, Field handles many very valuable operations which would otherwise fall on the
Expand All @@ -224,7 +219,7 @@ Second argument to addField() will contain a seed for the Field class::

You may also specify your own Field implementation::

$field = $this->addField('amount_and_currency', [MyAmountCurrencyField::class]);
$this->addField('amount_and_currency', [MyAmountCurrencyField::class]);

Read more about :php:class:`Field`

Expand Down Expand Up @@ -300,7 +295,6 @@ This can also be useful for calculating relative times::
$this->addCalculatedField('event_ts_human_friendly', ['expr' => function ($m) {
return $this->humanTiming($m->get('event_ts'));
}]);

}
}

Expand All @@ -311,17 +305,14 @@ Another common thing to define inside :php:meth:`Model::init()` would be
a user invokable actions::

class User extends Model {

function init(): void {

parent::init();

$this->addField('name');
$this->addField('email');
$this->addField('password');

$this->addUserAction('send_new_password');

}

function send_new_password()
Expand Down
4 changes: 0 additions & 4 deletions docs/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,14 @@ instance, the above expression will still be usable with any SQL vendor, but if
you want it to work with NoSQL, then your solution might be::

if ($model->hasMethod('addExpression')) {

// method is injected by Persistence
$model->addExpression('gross', ['expr' => '[net] + [vat]']);

} else {

// persistence does not support expressions
$model->addField('gross');
$model->onHook(Model::HOOK_BEFORE_SAVE, function ($m) {
$m->set('gross', $m->get('net') + $m->get('vat'));
});

}

Generic Persistence-code
Expand Down
3 changes: 0 additions & 3 deletions docs/persistence.rst
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,6 @@ Start by creating a beforeSave handler for Order::

$this->onHookShort(Model::HOOK_BEFORE_SAVE, function () {
if ($this->isDirty('ref')) {

if (
(new static())
->addCondition('client_id', $this->get('client_id')) // same client
Expand Down Expand Up @@ -514,7 +513,6 @@ to load records from two persistencies that are stored inside properties of my
application::

function loadQuick($class, $id) {

// first, try to load it from MemCache
$m = (clone $class)->setPersistence($this->mdb)->tryLoad($id);

Expand Down Expand Up @@ -768,7 +766,6 @@ Here is a way how to intervene with the process::
->setOrder('date desc')
->setLimit(1)
->action('field', ['total_gross'], 'getOne');

}, 'type' => 'float']);

The code above uses refLink and also creates expression, but it tweaks
Expand Down
7 changes: 4 additions & 3 deletions docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ It might be handy to use in-line definition of a model. Try the following
inside console::

$m = new \Atk4\Data\Model($db, 'contact_info');
$m->addFields(['address_1', 'address_2']);
$m->addField('address_1');
$m->addField('address_2');
$m->addCondition('address_1', '!=', null);
$m = $m->loadAny();
$m->get();
Expand All @@ -150,7 +151,8 @@ Next, exit and create file `src/Model_ContactInfo.php`::
{
parent::init();

$this->addFields(['address_1', 'address_2']);
$this->addField('address_1');
$this->addField('address_2');
$this->addCondition('address_1', '!=', null);
}
}
Expand Down Expand Up @@ -287,7 +289,6 @@ like this::
$j->addField('address_2');
$j->addField('address_3');
$j->hasOne('country_id', 'Country');

}
}

Expand Down
9 changes: 0 additions & 9 deletions docs/sql.rst
Original file line number Diff line number Diff line change
Expand Up @@ -166,19 +166,14 @@ Transactions
This method allows you to execute code within a 'START TRANSACTION / COMMIT' block::

class Invoice {

function applyPayment(Payment $p) {

$this->getPersistence()->atomic(function () use ($p) {

$this->set('paid', true);
$this->save();

$p->set('applied', true);
$p->save();

});

}
}

Expand Down Expand Up @@ -420,7 +415,6 @@ generates queries for most of model operations. By re-defining this method,
you can significantly affect the query building of an SQL model::

class CompanyProfit extends \Atk4\Data\Model {

public $companyId = null; // inject company ID, which will act as a condition/argument
public $readOnly = true; // instructs rest of the app, that this model is read-only

Expand All @@ -434,15 +428,13 @@ you can significantly affect the query building of an SQL model::
public function action($mode, $args = [])

if ($mode == 'select') {

// must return DSQL object here
return $this->expr('call get_company_profit([company_id])', [
'company_id' => $this->companyId,
]);
}

if ($mode == 'count') {

// optionally - expression for counting data rows, for pagination support
return $this->expr('select count(*) from (call get_company_profit([company_id]))', [
'company_id' => $this->companyId,
Expand Down Expand Up @@ -488,7 +480,6 @@ as an Model Source
Technically you can also specify expression as a $table property of your model::

class ClientReport extends \Atk4\Data\Model {

public $table = null; // will be set in init()
public $readOnly = true; // instructs rest of the app, that this model is read-only

Expand Down

0 comments on commit c6bfad1

Please sign in to comment.