Skip to content

Commit

Permalink
Several Important Fixes (#78)
Browse files Browse the repository at this point in the history
* Somewhere we forgot this. re-adding.

* $model->set('blah')  will set value to title field.

* save() can now combine set/save() by taking argument

* off-load insert() functionality to set()

* insert will return "id" instead of full model.

Because cloned models are not very reliable now.

* counter is part of dsql.

* Applied fixes from StyleCI

* fix some tests

* typo

* more tests

* style

* Add is_numeric

* More field validations

* Add more numeric field check

* style

* Implement hasOne() relationship ID binding.

* Applied fixes from StyleCI

* Implements intelligent model reloading after save()

* No need to complicate things.

* Add documentation

* Applied fixes from StyleCI

* Added afterUpdateQuery / afterInsertQuery

* Implement and document how to prevent default operations on active record

* remove outdated test (since it's no longer on readme)

* Added some really nice advanced topics.

* allow hasOne()->addField('foo'); (instead of saying foo twice)

* Applied fixes from StyleCI

* allow hasOne()->addField('foo'); (instead of saying foo twice)

* Applied fixes from StyleCI

* Improve exception catching in Persistence_SQL

* Use single-table update and single-table delete with where id=X

$m->save() invokes update with a fixed ID. Also it updates joined
tables separately. Because the record has been loaded successfully
before and will be reloaded after updates (in some cases) at a risk of
failed transaction, we don't have to invoke action here.

Action is more suited for all-set update and if you use joins with
SQLite you're on your own, as it does not support join with update.

Same goes for deletion.

* Remove commented out code.

* Expanded documentation.

* merge feature/allow-updating-id

* Revert "merge feature/allow-updating-id"

This reverts commit 77733ae.

* First we need ability to avoid field persistence

If model is saving field and join is saving on top, we get problem.

* Remove misleading test.

No manual tweaking of related values (until we can figure out how to do
it safely, through join)

* avoid clash between join and field save.

* Refactor SQL persistence to use save_buffer()

* implement afterUnload hook.

* make use of afterUnload correctly.

* still call update() on persistence if joins are dirty.

* do everything except actual query if no fields are changed.

* adjust test to verify unloading and changing only joined field.

* Fix problem introduced in pr #70

 #70 (comment)

* rename internal method

* implement isDirty()

* clean syntax.

* fix bug in docs.

* allow addFields(['foo','bar']) without associations.

* Validation improvement.

* addCondition('foo', ['a','b']); shouldn't touch default.

* style.

* Add support for array in $this->model

* add support for array/model definition.

* implement hasOne('foo_bar', ['class', 'default'=>123]);

* if table has alias, prepend it to sub-select aliases.

* ref('has_one_id') will no longer add weird condition if it's not loaded.

* Update test-scripts

* Style.

* style.
  • Loading branch information
romaninsh authored and DarkSide666 committed Jul 26, 2016
1 parent 1ea85b2 commit 3c3b6d9
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 38 deletions.
19 changes: 11 additions & 8 deletions src/Field_Many.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,17 @@ protected function getModel($defaults = [])
// last effort - try to add model
$p = $this->owner->persistence;

return $p->add($p->normalizeClassName($this->model, 'Model'), $defaults);

/* @todo These lines will never be executed !?
throw new Exception([
'Model is not defined for the relation',
'model' => $this->model,
]);
*/
if (is_array($this->model)) {
$model = $this->model[0];
$md = $this->model;
unset($md[0]);

$defaults = array_merge($md, $defaults);
} else {
$model = $this->model;
}

return $p->add($p->normalizeClassName($model, 'Model'), $defaults);
}

/**
Expand Down
64 changes: 35 additions & 29 deletions src/Field_One.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class Field_One
*/
protected $join = null;

protected $default = null;

/**
* Default constructor. Will copy argument into properties.
*
Expand Down Expand Up @@ -95,7 +97,7 @@ public function init()
$this->our_field = $this->link;
}
if (!$this->owner->hasElement($this->our_field)) {
$this->owner->addField($this->our_field, ['system' => true, 'join' => $this->join]);
$this->owner->addField($this->our_field, ['system' => true, 'join' => $this->join, 'default' => $this->default]);
}
}

Expand All @@ -113,6 +115,9 @@ public function getModel($defaults = [])
$this->table_alias = $this->link;
$this->table_alias = preg_replace('/_id/', '', $this->table_alias);
$this->table_alias = preg_replace('/([a-zA-Z])[a-zA-Z]*[^a-zA-Z]*/', '\1', $this->table_alias);
if (isset($this->owner->table_alias)) {
$this->table_alias = $this->owner->table_alias.'_'.$this->table_alias;
}
}
$defaults['table_alias'] = $this->table_alias;
}
Expand All @@ -139,14 +144,17 @@ public function getModel($defaults = [])
// last effort - try to add model
$p = $this->owner->persistence;

return $p->add($p->normalizeClassName($this->model, 'Model'), $defaults);
if (is_array($this->model)) {
$model = $this->model[0];
$md = $this->model;
unset($md[0]);

/* @todo These lines will never be executed !?
throw new Exception([
'Model is not defined for the relation',
'model' => $this->model,
]);
*/
$defaults = array_merge($md, $defaults);
} else {
$model = $this->model;
}

return $p->add($p->normalizeClassName($model, 'Model'), $defaults);
}

/**
Expand All @@ -173,30 +181,28 @@ protected function referenceOurValue()
public function ref($defaults = [])
{
$m = $this->getModel($defaults);
if ($this->owner->loaded()) {
if ($this->their_field) {
return $m->tryLoadBy($this->their_field, $this->owner[$this->our_field])
->addHook('afterSave', function ($m) {
$this->owner[$this->our_field] = $m[$this->their_field];
})
->addHook('afterDelete', function ($m) {
$this->owner[$this->our_field] = null;
});
} else {
return $m->tryLoad($this->owner[$this->our_field])
->addHook('afterSave', function ($m) {
$this->owner[$this->our_field] = $m->id;
})
->addHook('afterDelete', function ($m) {
$this->owner[$this->our_field] = null;
});
$m->addHook('afterDelete', function ($m) {
$this->owner[$this->our_field] = null;
});

if ($this->their_field) {
if ($this->owner[$this->our_field]) {
$m->tryLoadBy($this->their_field, $this->owner[$this->our_field]);
}
} else {
$m = clone $m; // we will be adding conditions!

$values = $this->owner->action('field', [$this->our_field]);
return
$m->addHook('afterSave', function ($m) {
$this->owner[$this->our_field] = $m[$this->their_field];
});
} else {
if ($this->owner[$this->our_field]) {
$m->tryLoad($this->owner[$this->our_field]);
}

return $m->addCondition($this->their_field ?: $m->id_field, $values);
return
$m->addHook('afterSave', function ($m) {
$this->owner[$this->our_field] = $m->id;
});
}
}

Expand Down
11 changes: 10 additions & 1 deletion tests/RelationSQLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function testBasicOne()
$o->addCondition('amount', '<', 9);

$this->assertEquals(
'select `id`,`name` from `user` where `id` in (select `user_id` from `order` where `amount` > :a and `amount` < :b)',
'select `id`,`name` from `user`',
$o->ref('user_id')->action('select')->render()
);
}
Expand Down Expand Up @@ -300,4 +300,13 @@ public function testRelationHook()
$this->assertEquals('Peters new contact', $u->ref('contact_id')['address']);
$this->assertEquals('Peters new contact', $u['address']);
}

public function testModelProperty()
{
$db = new Persistence_SQL($this->db->connection);
$user = new Model($db, ['table' => 'user']);
$user->hasMany('Orders', ['model' => ['atk4/data/Model', 'table' => 'order'], 'their_field' => 'id']);
$o = $user->ref('Orders');
$this->assertEquals('order', $o->table);
}
}
11 changes: 11 additions & 0 deletions tests/RelationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace atk4\data\tests;

use atk4\data\Model;
use atk4\data\Persistence;

/**
* @coversDefaultClass \atk4\data\Model
Expand Down Expand Up @@ -36,4 +37,14 @@ public function testBasicRelations()

$this->assertEquals(100, $user->ref('BigOrders')['amount']);
}

public function testModelProperty()
{
$db = new Persistence();
$user = new Model($db, ['table' => 'user']);
$user->id = 1;
$user->hasOne('order_id', ['model' => ['atk4/data/Model', 'table' => 'order']]);
$o = $user->ref('order_id');
$this->assertEquals('order', $o->table);
}
}

0 comments on commit 3c3b6d9

Please sign in to comment.