Skip to content

Commit

Permalink
Minor refactor in insert() and save() (#58)
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

* Applied fixes from StyleCI
  • Loading branch information
romaninsh authored and DarkSide666 committed Jul 21, 2016
1 parent 74f298f commit 94e938b
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Field_Many.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected function getModel($defaults = [])
if (is_object($this->model) && $this->model instanceof \Closure) {
$c = $this->model;

$c = $c($this->owner, $this);
$c = $c($this->owner, $this, $defaults);
if (!$c->persistence && $this->owner->persistence) {
$c = $this->owner->persistence->add($c, $defaults);
}
Expand Down
38 changes: 18 additions & 20 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ private function normalizeFieldName($field)
$field = $field->short_name;
}

if (!is_string($field)) {
if (!is_string($field) || $field === '' || is_numeric($field[0])) {
throw new Exception([
'Incorect specification of field name',
'arg' => $field,
Expand All @@ -277,20 +277,21 @@ private function normalizeFieldName($field)

public function set($field, $value = null)
{
// set(['foo'=>'bar']) will call itself as set('foo', 'bar');
if (func_num_args() == 1) {
if (is_array($field)) {
foreach ($field as $key => $value) {
$this->set($key, $value);
if ($key === '0' || $key === 0) {
$this->set($value);
} else {
$this->set($key, $value);
}
}

return $this;
} else {
$value = $field;
$field = $this->title_field;
}

throw new Exception([
'Single argument set() requires an array argument',
'arg' => $field,
]);
}

$field = $this->normalizeFieldName($field);
Expand Down Expand Up @@ -658,12 +659,16 @@ public function tryLoadBy($field, $value)
return $this;
}

public function save()
public function save($data = [])
{
if (!$this->persistence) {
throw new Exception(['Model is not associated with any database']);
}

if ($data) {
$this->set($data);
}

if ($this->hook('beforeSave') === false) {
return $this;
}
Expand Down Expand Up @@ -704,6 +709,7 @@ public function save()

//$this->hook('beforeUpdate', array(&$source));
} else {
$data = [];
foreach ($this->get() as $name => $value) {
$field = $this->hasElement($name);
if (!$field) {
Expand Down Expand Up @@ -747,16 +753,8 @@ public function save()
protected function _rawInsert($m, $row)
{
$m->unload();
if (!is_array($row)) {
$m->set($this->title_field, $row);
} else {
if (isset($row[0]) && $this->title_field) {
$row[$this->title_field] = $row[0];
unset($row[0]);
}
$m->set($row);
}
$m->save();
$m->save($row);
$m->data[$m->id_field] = $m->id;
}

/**
Expand All @@ -769,7 +767,7 @@ public function insert($row)
$m = clone $this;
$this->_rawInsert($m, $row);

return $m;
return $m->id;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Persistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static function connect($dsn, $user = null, $password = null, $args = [])
switch (strtolower(isset($args['driver']) ?: $driver)) {
case 'mysql':
case 'dumper':
case 'counter':
case 'sqlite':
return new Persistence_SQL($dsn, $user, $password, $args);
default:
Expand Down
67 changes: 65 additions & 2 deletions tests/BusinessModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* @coversDefaultClass \atk4\data\Model
*/
class BusinessModelTest extends \PHPUnit_Framework_TestCase
class BusinessModelTest extends TestCase
{
/**
* Test constructor.
Expand Down Expand Up @@ -143,13 +143,76 @@ public function testException1fixed()
$m['name'] = 5;
}

/**
* Sets title field.
*/
public function testSetTitle()
{
$m = new Model();
$m->addField('name');
$m->set('foo');
$this->assertEquals($m['name'], 'foo');

$m->set(['bar']);
$this->assertEquals($m['name'], 'bar');

$m->set(['name' => 'baz']);
$this->assertEquals($m['name'], 'baz');
}

/**
* @expectedException Exception
*
* fields can't be numeric
*/
public function testException2()
{
$m = new Model();
$m->set('foo');
$m->set(0, 'foo');
}

/**
* @expectedException Exception
*
* fields can't be numeric
*/
public function testException2a()
{
$m = new Model();
$m->set('3', 'foo');
}

/**
* @expectedException Exception
*
* fields can't be numeric
*/
public function testException2b()
{
$m = new Model();
$m->set('3b', 'foo');
}

/**
* @expectedException Exception
*
* fields can't be numeric
*/
public function testException2c()
{
$m = new Model();
$m->set('', 'foo');
}

/**
* @expectedException Exception
*
* fields can't be numeric
*/
public function testException2d()
{
$m = new Model();
$m->set(['foo', 'bar']);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/PersistentSQLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ public function testModelInsert()
$ms[] = $m->insert($row);
}

$this->assertEquals('John', $ms[0]['name']);
$this->assertEquals('John', $m->load($ms[0])['name']);

$this->assertEquals('Jones', $ms[1]['surname']);
$this->assertEquals('Jones', $m->load($ms[1])['surname']);
}

public function testModelInsertRows()
Expand Down

0 comments on commit 94e938b

Please sign in to comment.