Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename all remaining properties to camelCase #1043

Merged
merged 8 commits into from
Aug 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,11 @@ class JobReport extends Job {

// build relation between job and invoice line
$this->hasMany('InvoiceLines', ['model' => $invoiceLines])
->addField('invoiced', ['aggregate' => 'sum', 'field' => 'total', 'type' => 'atk4_money']);
->addField('invoiced', [
'aggregate' => 'sum',
'field' => 'total',
'type' => 'atk4_money'
]);

// next we need to see how much is reported through timesheets
$timesheet = new Timesheet($this->getPersistence());
Expand All @@ -153,10 +157,11 @@ class JobReport extends Job {

// build relation between Job and Timesheets
$this->hasMany('Timesheets', ['model' => $timesheet])
->addField(
'reported',
['aggregate' => 'sum', 'field' => 'cost', 'type' => 'atk4_money']
);
->addField('reported', [
'aggregate' => 'sum',
'field' => 'cost',
'type' => 'atk4_money'
]);

// finally lets calculate profit
$this->addExpression('profit', ['expr' => '[invoiced] - [reported]']);
Expand Down Expand Up @@ -339,12 +344,12 @@ Resulting queries (I have removed back-ticks and parametric variables for readab
- `category_id` can be looked up directly inside the INSERT (standard feature of SQL reference fields).

```sql
select id, name from client where name = "Pear Company" and is_deleted = 0;
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.5),
(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 Down Expand Up @@ -502,7 +507,7 @@ If you wonder how those advanced features may impact performance of loading and

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

// $project refers to same object at all times, but $project's active data
Expand Down
16 changes: 8 additions & 8 deletions docs/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ which I want to define like this::
In order to add your defined behavior to the model. The first check actually
allows you to define models that will bypass audit altogether::

$u1 = new Model_User($db); // Model_User::init() includes audit
$u1 = new Model_User($db); // Model_User::init() includes audit

$u2 = new Model_User($db, ['no_audit' => true]); // will exclude audit features
$u2 = new Model_User($db, ['no_audit' => true]); // will exclude audit features

Next we are going to define 'created_dts' field which will default to the
current date and time.
Expand Down Expand Up @@ -426,7 +426,7 @@ inside your model are unique::

// by default make 'name' unique
if (!$this->fields) {
$this->fields = [$this->getOwner()->title_field];
$this->fields = [$this->getOwner()->titleField];
}

$this->getOwner()->onHook(Model::HOOK_BEFORE_SAVE, \Closure::fromCallable([$this, 'beforeSave']));
Expand All @@ -437,7 +437,7 @@ inside your model are unique::
foreach ($this->fields as $field) {
if ($m->getDirtyRef()[$field]) {
$mm = clone $m;
$mm->addCondition($mm->id_field != $this->id);
$mm->addCondition($mm->idField != $this->id);
$mm = $mm->tryLoadBy($field, $m->get($field));

if ($mm !== null) {
Expand Down Expand Up @@ -520,7 +520,7 @@ Next we need to define reference. Inside Model_Invoice add::
$j = $p->join('invoice_payment.payment_id');
$j->addField('amount_closed');
$j->hasOne('invoice_id', 'Model_Invoice');
}, 'their_field' => 'invoice_id']);
}, 'theirField' => 'invoice_id']);

$this->onHookShort(Model::HOOK_BEFORE_DELETE, function () {
foreach ($this->ref('InvoicePayment') as $payment) {
Expand Down Expand Up @@ -670,7 +670,7 @@ persistence layer to load or save anything. Next I need a beforeSave handler::

if($this->_isset('category') && !$this->_isset('category_id')) {
$c = $this->refModel('category_id');
$c->addCondition($c->title_field, 'like', $this->get('category'));
$c->addCondition($c->titleField, 'like', $this->get('category'));
$this->set('category_id', $c->action('field', ['id']));
}
});
Expand All @@ -689,7 +689,7 @@ What if the user-specified entry is not found? Lets look at the code::

if($m->_isset('category') && !$m->_isset('category_id')) {
$c = $this->refModel('category_id');
$c->addCondition($c->title_field, 'like', $m->get('category'));
$c->addCondition($c->titleField, 'like', $m->get('category'));
$m->set('category_id', $c->action('field', ['id']));
}

Expand All @@ -698,7 +698,7 @@ If you wish to use a different value instead, you can create an expression::

if($m->_isset('category') && !$m->_isset('category_id')) {
$c = $this->refModel('category_id');
$c->addCondition($c->title_field, 'like', $m->get('category'));
$c->addCondition($c->titleField, 'like', $m->get('category'));
$m->set('category_id', $this->expr('coalesce([], [])', [
$c->action('field', ['id']),
$m->getField('category_id')->default,
Expand Down
10 changes: 5 additions & 5 deletions docs/conditions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ either explicitly or through a $table property inside a model::

$m = new Model_User($db, 'user');
$m = $m->load(1);
echo $m->get('gender'); // "M"
echo $m->get('gender'); // "M"


Following this line, you can load ANY record from the table. It's possible to
Expand All @@ -36,17 +36,17 @@ Basic Usage
There are many ways to execute addCondition. The most basic one that will be
supported by all the drivers consists of 2 arguments or if operator is '='::

$m->addCondition('gender', 'F'); // or
$m->addCondition('gender', '=', 'F');
$m->addCondition('gender', 'F');
$m->addCondition('gender', '=', 'F'); // exactly same

Once you add a condition, you can't get rid of it, so if you want
to preserve the state of your model, you need to use clone::

$m = new Model_User($db, 'user');
$girls = (clone $m)->addCondition('gender', 'F');

$m = $m->load(1); // success
$girls = $girls->load(1); // exception
$m = $m->load(1); // success
$girls = $girls->load(1); // exception

Operations
----------
Expand Down
4 changes: 2 additions & 2 deletions docs/expressions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ inside your model, however you can affect this behavior::

$m->save();

echo $m->get('sum'); // outputs null
echo $m->get('sum'); // outputs null

$m->reload();
echo $m->get('sum'); // outputs 10
echo $m->get('sum'); // outputs 10

Now it requires an explicit reload for your model to fetch the result. There
is another scenario when your database defines default fields:
Expand Down
8 changes: 4 additions & 4 deletions docs/fields.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ can set and read value of that field::
$model->addField('name');
$model->set('name', 'John');

echo $model->get('name'); // john
echo $model->get('name'); // John

Just like you can reuse :php:class:`Model` to access multiple data records,
:php:class:`Field` object will be reused also.
Expand All @@ -54,9 +54,9 @@ Field Type
Probably a most useful quality of Field is that it has a clear type::

$model->addField('age', ['type' => 'integer']);
$model->set('age', "123");
$model->set('age', '123');

var_dump($model->get('age')); // int(123)
var_dump($model->get('age')); // int(123)

Agile Data defines some basic types to make sure that values:

Expand Down Expand Up @@ -144,7 +144,7 @@ Example::
$model->set('age', 0);
$model->save();

$model->set('age', null); // exception
$model->set('age', null); // exception


.. php:attr:: required
Expand Down
10 changes: 5 additions & 5 deletions docs/joins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,17 @@ assume that it's a reverse join.

Reverse joins are saved in the opposite order - primary table will be saved
first and when id of a primary table is known, foreign table record is stored
and ID is supplied. You can pass option 'master_field' to the join() which will
and ID is supplied. You can pass option 'masterField' to the join() which will
specify which field to be used for matching. By default the field is calculated
like this: foreignTable . '_id'. Here is usage example::

$user->addField('username');
$jCreditCard = $user->join('credit_card', [
'prefix' => 'cc_',
'master_field' => 'default_credit_card_id',
'masterField' => 'default_credit_card_id',
]);
$jCreditCard->addField('integer'); // creates cc_number
$jCreditCard->addField('name'); // creates cc_name
$jCreditCard->addField('integer'); // creates cc_number
$jCreditCard->addField('name'); // creates cc_name

Master field can also be specified as an object of a Field class.

Expand Down Expand Up @@ -139,7 +139,7 @@ with a foreign table.
.. php:method:: hasMany

same as :php:meth:`Model::hasMany` but condition for related model will be
based on foreign table field and :php:attr:`Reference::their_field` will be
based on foreign table field and :php:attr:`Reference::theirField` will be
set to $foreignTable . '_id'.

.. php:method:: containsOne
Expand Down
46 changes: 23 additions & 23 deletions docs/model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,10 @@ a hook::
Now if you attempt to save object, you will receive :php:class:`ValidationException`::

$model->set('name', 'Swift');
$model->saveAndUnload(); // all good
$model->saveAndUnload(); // all good

$model->set('name', 'C#');
$model->saveAndUnload(); // exception here
$model->saveAndUnload(); // exception here


Other Uses
Expand Down Expand Up @@ -423,15 +423,15 @@ explicitly associate model with persistence like this::

// ....

$m->setPersistence($db); // links with persistence
$m->setPersistence($db); // links with persistence

Multiple models can be associated with the same persistence. Here are also some examples
of static persistence::

$m = new Model(new Persistence\Static_(['john', 'peter', 'steve']);

$m = $m->load(1);
echo $m->get('name'); // peter
echo $m->get('name'); // peter

See :php:class:`Persistence\\Static_`

Expand Down Expand Up @@ -606,30 +606,30 @@ Full example::
// Fields can be added after model is created
$m->addField('salary', ['default' => 1000]);

echo $m->_isset('salary'); // false
echo $m->get('salary'); // 1000
echo $m->_isset('salary'); // false
echo $m->get('salary'); // 1000

// Next we load record from $db
$m = $m->load(1);

echo $m->get('salary'); // 2000 (from db)
echo $m->_isset('salary'); // false, was not changed
echo $m->get('salary'); // 2000 (from db)
echo $m->_isset('salary'); // false, was not changed

$m->set('salary', 3000);

echo $m->get('salary'); // 3000 (changed)
echo $m->_isset('salary'); // true
echo $m->get('salary'); // 3000 (changed)
echo $m->_isset('salary'); // true

$m->_unset('salary'); // return to original value
$m->_unset('salary'); // return to original value

echo $m->get('salary'); // 2000
echo $m->_isset('salary'); // false
echo $m->get('salary'); // 2000
echo $m->_isset('salary'); // false

$m->set('salary', 3000);
$m->save();

echo $m->get('salary'); // 3000 (now in db)
echo $m->_isset('salary'); // false
echo $m->get('salary'); // 3000 (now in db)
echo $m->_isset('salary'); // false

.. php:method:: protected normalizeFieldName

Expand All @@ -642,40 +642,40 @@ Those are three properties that you can specify in the model or pass it through
defaults::

class MyModel ..
public $title_field = 'full_name';
public ?string $titleField = 'full_name';

or as defaults::

$m = new MyModel($db, ['title_field' => 'full_name']);
$m = new MyModel($db, ['titleField' => 'full_name']);


.. _id_field:
.. _idField:

ID Field
--------

.. php:attr:: id_field
.. php:attr:: idField

If your data storage uses field different than ``id`` to keep the ID of your
records, then you can specify that in $id_field property.
records, then you can specify that in $idField property.

ID value of loaded entity cannot be changed. If you want to duplicate a record,
you need to create a new entity and save it.

.. _title_field:
.. _titleField:

Title Field
-----------

.. php:attr:: title_field
.. php:attr:: titleField

This field by default is set to 'name' will act as a primary title field of
your table. This is especially handy if you use model inside UI framework,
which can automatically display value of your title field in the header,
or inside drop-down.

If you don't have field 'name' but you want some other field to be title,
you can specify that in the property. If title_field is not needed, set it
you can specify that in the property. If titleField is not needed, set it
to false or point towards a non-existent field.

See: :php:meth::`hasOne::addTitle()`
Expand Down
4 changes: 2 additions & 2 deletions docs/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ using::

$user = new User($db);

$user = $user->load(20); // load specific user record into PHP
echo $user->get('name') . ': '; // access field values
$user = $user->load(20); // load specific user record into PHP
echo $user->get('name') . ': '; // access field values

$gross = $user->ref('Invoice')
->addCondition('status', 'due')
Expand Down
Loading