Skip to content

Commit

Permalink
Merge branch 'develop' into action_to_toquery_with_interfaces
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Model.php
#	src/Persistence/Array_.php
#	src/Persistence/Csv.php
#	src/Persistence/Sql.php
#	src/Reference/ContainsMany.php
#	src/Reference/ContainsOne.php
#	tests/ContainsOneTest.php
#	tests/CsvTest.php
  • Loading branch information
georgehristov committed Sep 11, 2020
2 parents 22f5dbf + 68b068f commit fbff6c0
Show file tree
Hide file tree
Showing 52 changed files with 553 additions and 504 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ jobs:
if: matrix.type == 'Phpunit'
run: "vendor/bin/phpunit --configuration phpunit-mysql.xml.dist \"$(if [ -n \"$LOG_COVERAGE\" ]; then echo '--coverage-text'; else echo '--no-coverage'; fi)\" -v"

- name: "Run tests: PostgreSQL (expected errors) (only for Phpunit)"
- name: "Run tests: PostgreSQL (only for Phpunit)"
if: matrix.type == 'Phpunit'
continue-on-error: true
run: "vendor/bin/phpunit --configuration phpunit-pgsql.xml.dist \"$(if [ -n \"$LOG_COVERAGE\" ]; then echo '--coverage-text'; else echo '--no-coverage'; fi)\" -v"

- name: Lint / check syntax (only for CodingStyle)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ You can, however, [import fields through joins too](http://agile-data.readthedoc

#### Q: I don't like the `$book->set('field', 123)`, I prefer properties

Agile Models are not Entities. They don't represent a single record, but rather a set of records. Which is why Model has some important properties: `$model->id`, `$model->persistence` and `model->data`.
Agile Models are not Entities. They don't represent a single record, but rather a set of records. Which is why Model has some important properties: `$model->getId()`, `$model->persistence` and `model->data`.

Read more on [working with individual data records](http://agile-data.readthedocs.io/en/develop/persistence.html).

Expand Down
24 changes: 12 additions & 12 deletions docs/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ hook. Place the following inside Transaction::init()::
if (get_class($this) != $m->getClassName()) {
$cl = '\\'.$this->getClassName();
$cl = new $cl($this->persistence);
$cl->load($m->id);
$cl->load($m->getId());

$this->breakHook($cl);
}
Expand Down Expand Up @@ -159,7 +159,7 @@ which I want to define like this::

$this->owner->hasOne('created_by_user_id', 'User');
if(isset($this->app->user) and $this->app->user->loaded()) {
$this->owner->getField('created_by_user_id')->default = $this->app->user->id;
$this->owner->getField('created_by_user_id')->default = $this->app->user->getId();
}

$this->owner->hasOne('updated_by_user_id', 'User');
Expand All @@ -168,7 +168,7 @@ which I want to define like this::

$this->owner->onHook(Model::HOOK_BEFORE_UPDATE, function($m, $data) {
if(isset($this->app->user) and $this->app->user->loaded()) {
$data['updated_by'] = $this->app->user->id;
$data['updated_by'] = $this->app->user->getId();
}
$data['updated_dts'] = new \DateTime();
});
Expand Down Expand Up @@ -243,7 +243,7 @@ Start by creating a class::
throw (new \atk4\core\Exception('Model must be loaded before soft-deleting'))->addMoreInfo('model', $m);
}

$id = $m->id;
$id = $m->getId();
if ($m->hook('beforeSoftDelete') === false) {
return $m;
}
Expand All @@ -262,7 +262,7 @@ Start by creating a class::
throw (new \atk4\core\Exception(['Model must be loaded before restoring'))->addMoreInfo('model', $m);
}

$id = $m->id;
$id = $m->getId();
if ($m->hook('beforeRestore') === false) {
return $m;
}
Expand Down Expand Up @@ -352,7 +352,7 @@ before and just slightly modifying it::
throw (new \atk4\core\Exception('Model must be loaded before soft-deleting'))->addMoreInfo('model', $m);
}

$id = $m->id;
$id = $m->getId();

$rs = $m->reload_after_save;
$m->reload_after_save = false;
Expand All @@ -369,7 +369,7 @@ before and just slightly modifying it::
throw (new \atk4\core\Exception('Model must be loaded before restoring'))->addMoreInfo('model', $m);
}

$id = $m->id;
$id = $m->getId();
if ($m->hook('beforeRestore') === false) {
return $m;
}
Expand Down Expand Up @@ -584,7 +584,7 @@ payment towards a most suitable invoice::

// How much we can allocate to this invoice
$alloc = min($this->get('amount_due'), $invoices->get('amount_due'))
$this->ref('InvoicePayment')->insert(['amount_closed'=>$alloc, 'invoice_id'=>$invoices->id]);
$this->ref('InvoicePayment')->insert(['amount_closed'=>$alloc, 'invoice_id'=>$invoices->getId()]);

// Reload ourselves to refresh amount_due
$this->reload();
Expand Down Expand Up @@ -629,8 +629,8 @@ API call) this approach will require us to perform 2 extra queries::
$m = new Model_Invoice($db);
$m->insert([
'total'=>20,
'client_id'=>$m->ref('client_id')->loadBy('code', $client_code)->id,
'category_id'=>$m->ref('category_id')->loadBy('name', $category)->id,
'client_id'=>$m->ref('client_id')->loadBy('code', $client_code)->getId(),
'category_id'=>$m->ref('category_id')->loadBy('name', $category)->getId(),
]);

The ideal way would be to create some "non-persistable" fields that can be used
Expand Down Expand Up @@ -806,14 +806,14 @@ field only to offer payments made by the same client. Inside Model_Invoice add::
$m = new Model_Invoice($db);
$m->set('client_id', 123);

$m->set('payment_invoice_id', $m->ref('payment_invoice_id')->tryLoadAny()->id);
$m->set('payment_invoice_id', $m->ref('payment_invoice_id')->tryLoadAny()->getId());

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) {

$m->set('payment_invoice_id', $m->ref('payment_invoice_id')->tryLoadAny()->id);
$m->set('payment_invoice_id', $m->ref('payment_invoice_id')->tryLoadAny()->getId());
$m->save();

});
Expand Down
2 changes: 1 addition & 1 deletion docs/expressions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ In this example the query will look like this:
select (1) `id`, (now()) `now` limit 1
so that ``$m->id`` will always be 1 which will make it a model that you can
so that ``$m->getId()`` will always be 1 which will make it a model that you can
actually use consistently throughout the system. The real benefit from this
can be gained when you need to pull various statistical values from your
database at once::
Expand Down
2 changes: 1 addition & 1 deletion docs/hooks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ the database. Here is how you can implement this functionality::
$data = $m->app->cacheFetch($m->table, $id);
if ($data) {
$m->data = $data;
$m->id = $id;
$m->setId($id);
$m->breakHook(false);
}
});
Expand Down
2 changes: 1 addition & 1 deletion docs/joins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ Joins are implemented like this:
table by default driver
- join will add either `beforeInsert` or `afterInsert` hook inside your model.
When save is executed, it will execute additional query to update foreign table.
- while $model->id stores the ID of the main table active record, $join->id
- while $model->getId() stores the ID of the main table active record, $join->id
stores ID of the foreign record and will be used when updating.
- option 'delete_behaviour' is 'cascade' for strong joins and 'ignore' for weak
joins, but you can set some other value. If you use "setnull" value and you
Expand Down
2 changes: 1 addition & 1 deletion docs/model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ ID Field
This will update existing record with new $id. If you want to save your
current field over another existing record then::

$m->id = $new_id;
$m->setId($new_id);
$m->save();

You must remember that only dirty fields are saved, though. (We might add
Expand Down
10 changes: 5 additions & 5 deletions docs/persistence.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ There are several ways to link your model up with the persistence::

$m->load(10);
$m->set('name', 'John');
$$m->save();
$m->save();

You can pass argument to save() to set() and save()::

Expand Down Expand Up @@ -117,7 +117,7 @@ However if you change the ID for record that was loaded, then your database
record will also have its ID changed. Here is example::

$m->load(123);
$m->set($m->id_field, 321);
$m->setId(321);
$m->save();

After this your database won't have a record with ID 123 anymore.
Expand Down Expand Up @@ -497,7 +497,7 @@ Start by creating a beforeSave handler for Order::
if (
$m->newInstance()
->addCondition('client_id', $m->get('client_id')) // same client
->addCondition($m->id_field, '!=', $m->id) // has another order
->addCondition($m->id_field, '!=', $m->getId()) // has another order
->tryLoadBy('ref', $m->get('ref')) // with same ref
->loaded()
) {
Expand Down Expand Up @@ -553,7 +553,7 @@ The other, more appropriate option is to re-use a vanilla Order record::
$this->save(); // just to be sure, no dirty stuff is left over

$archive = $this->newInstance();
$archive->load($this->id);
$archive->load($this->getId());
$archive->set('is_archived', true);

$this->unload(); // active record is no longer accessible
Expand Down Expand Up @@ -781,7 +781,7 @@ some other database (for archive purposes) you can implement it like this::
$arc = $this->withPersistence($m->app->archive_db, false);

// add some audit fields
$arc->addField('original_id')->set($this->id);
$arc->addField('original_id')->set($this->getId());
$arc->addField('saved_by')->set($this->app->user);

$arc->saveAndUnload();
Expand Down
4 changes: 2 additions & 2 deletions docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ You can load / unload records like this::
$m->set('email', '[email protected]');
$m->save();

You can call `$m->loaded()` to see if there is active record and `$m->id` will
You can call `$m->loaded()` to see if there is active record and `$m->getId()` will
store the ID of active record. You can also un-load the record with `$m->unload()`.

By default no records are loaded and if you modify some field and attempt
Expand Down Expand Up @@ -491,7 +491,7 @@ This results in an instance of Model_Country with Active Record set to the
country of user john::

$cc->loaded();
$cc->id;
$cc->getId();
$cc->get();

Implementation of References
Expand Down
2 changes: 1 addition & 1 deletion docs/references.rst
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ No condition will be applied by default so it's all up to you::
$m->addField('original_id', ['type' => 'int']);

if ($m->loaded)) {
$archive->addCondition('original_id', $m->id);
$archive->addCondition('original_id', $m->getId());
// only show record of currently loaded record
}
});
Expand Down
4 changes: 2 additions & 2 deletions docs/sql.rst
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ You should be familiar with http://dsql.readthedocs.io/en/develop/expressions.ht
In short this should allow you to build and execute any SQL statement::

$this->expr("call get_nominal_sheet([],[],'2014-10-01','2015-09-30',0)", [
$this->app->system->id,
$this->app->system->getId(),
$this->app->system['contractor_id']
])->execute();

Expand Down Expand Up @@ -487,7 +487,7 @@ procedure inside Model::init() then set $table property to a temporary table::
parent::init();

$q = $this->expr("call get_nominal_sheet([],[],'2014-10-01','2015-09-30',0)", [
$this->app->system->id,
$this->app->system->getId(),
$this->app->system['contractor_id']
])->execute();

Expand Down
7 changes: 6 additions & 1 deletion src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,12 @@ public function setNull(): self
*/
public function compare($value): bool
{
return $this->get() == $value;
if ($this->owner->persistence === null) {
return (string) $this->normalize($this->get()) === (string) $this->normalize($value);
}

return (string) $this->owner->persistence->typecastSaveRow($this->owner, [$this->short_name => $this->get()])[$this->short_name]
=== (string) $this->owner->persistence->typecastSaveRow($this->owner, [$this->short_name => $value])[$this->short_name];
}

/**
Expand Down
Loading

0 comments on commit fbff6c0

Please sign in to comment.