Skip to content

Commit

Permalink
Merge pull request #86 from atk4/feature/rename-classes
Browse files Browse the repository at this point in the history
Feature/rename classes etc.
  • Loading branch information
romaninsh committed Aug 3, 2016
2 parents c877f11 + f078b19 commit 09c76a1
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 33 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# upcomming

* Change: classes Field_One, Field_Many and Field_SQL_One are renamed to Relation_One, Relation_Many and Relation_SQL_One respectively.



# 1.0.1

This is our first maintenance release that solves several important issues.
Expand Down
30 changes: 16 additions & 14 deletions docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -484,20 +484,22 @@ user john::
Implementation of Relations
---------------------------

When relation is added using getOne or getMany, the new object is created and added
into Model of class Field_Many or Field_One (or Field_SQL_One). The object itself
is quite simple and you can fetch it form the model if you keep the return value
of hasOne() / hasMany() or call getRef() with the same identifier later on.

Calling ref() will proxy into the ref() method of relation object which will
in turn figure out what to do.

Additionally you can call addField() on the reference model that will bring
one or several fields from related model into your current model.

Finally this reference object contains method getModel() which will produce
a (possibly) fresh copy of related entity and will either adjust it's
DataSet or set the active record.
When relation is added using :php:meth:`Model::hasOne()` or :php:meth:`Model::hasMany()`,
the new object is created and added into Model of class :php:class:`Relation_Many`
or :php:class:`Relation_One` (or :php:class:`Relation_SQL_One` in case you use SQL
database). The object itself is quite simple and you can fetch it form the model if
you keep the return value of hasOne() / hasMany() or call :php:meth:`Model::getRef()`
with the same identifier later on.

Calling :php:meth:`Model::ref()` will proxy into the ref() method of relation
object which will in turn figure out what to do.

Additionally you can call :php:meth:`Model::addField()` on the reference model
that will bring one or several fields from related model into your current model.

Finally this reference object contains method getModel() which will produce a
(possibly) fresh copy of related entity and will either adjust it's DataSet or
set the active record.

Actions
=======
Expand Down
4 changes: 4 additions & 0 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ public function __debugInfo()
$arr['join'] = $this->join;
}

if ($this->editable) {
$arr['editable'] = $this->editable;
}

return $arr;
}

Expand Down
18 changes: 7 additions & 11 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ class Model implements \ArrayAccess, \IteratorAggregate
*
* @var string
*/
public $_default_class_hasOne = 'atk4\data\Field_One';
public $_default_class_hasOne = 'atk4\data\Relation_One';

/**
* The class used by hasMany() method.
*
* @var string
*/
public $_default_class_hasMany = 'atk4\data\Field_Many';
public $_default_class_hasMany = 'atk4\data\Relation_Many';

/**
* The class used by addField() method.
Expand Down Expand Up @@ -259,7 +259,7 @@ public function init()
$this->_init();

if ($this->id_field) {
$this->addField($this->id_field, ['system' => true, 'type' => 'int']);
$this->addField($this->id_field, ['system' => true, 'type' => 'int', 'editable' => false]);
}
}

Expand Down Expand Up @@ -1234,18 +1234,14 @@ public function leftJoin($foreign_table, $defaults = [])
*
* @param string $c Class name
* @param string $link Link
* @param array $defaults Properties
* @param array $defaults Properties which we will pass to Relation object constructor
*
* @return object
*/
protected function _hasRelation($c, $link, $defaults = [])
{
if (!is_array($defaults)) {
if ($defaults) {
$defaults = ['model' => $defaults];
} else {
$defaults = ['model' => 'Model_'.$link];
}
$defaults = ['model' => $defaults ?: 'Model_'.$link];
} elseif (isset($defaults[0])) {
$defaults['model'] = $defaults[0];
unset($defaults[0]);
Expand All @@ -1262,7 +1258,7 @@ protected function _hasRelation($c, $link, $defaults = [])
* @param string $link
* @param array $defaults
*
* @return Field_One
* @return Relation_One
*/
public function hasOne($link, $defaults = [])
{
Expand All @@ -1275,7 +1271,7 @@ public function hasOne($link, $defaults = [])
* @param string $link
* @param array $defaults
*
* @return Field_Many
* @return Relation_Many
*/
public function hasMany($link, $defaults = [])
{
Expand Down
4 changes: 2 additions & 2 deletions src/Persistence_SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ class Persistence_SQL extends Persistence
*
* @var string
*/
public $_default_class_hasOne = 'atk4\data\Field_SQL_One';
public $_default_class_hasOne = 'atk4\data\Relation_SQL_One';

/**
* Default class when adding hasMany field.
*
* @var string
*/
public $_default_class_hasMany = null; //'atk4\data\Field_Many';
public $_default_class_hasMany = null; //'atk4\data\Relation_Many';

/**
* Default class when adding Expression field.
Expand Down
2 changes: 1 addition & 1 deletion src/Field_Many.php → src/Relation_Many.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* Class description?
*/
class Field_Many
class Relation_Many
{
use \atk4\core\TrackableTrait {
init as _init;
Expand Down
29 changes: 25 additions & 4 deletions src/Field_One.php → src/Relation_One.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* Class description?
*/
class Field_One
class Relation_One
{
use \atk4\core\InitializerTrait {
init as _init;
Expand Down Expand Up @@ -58,7 +58,19 @@ class Field_One
*/
protected $join = null;

protected $default = null;
/**
* Default value of field.
*
* @var mixed
*/
public $default = null;

/**
* Is field editable? Normally you can edit fields.
*
* @var bool
*/
public $editable = true;

/**
* Default constructor. Will copy argument into properties.
Expand All @@ -78,7 +90,7 @@ public function __construct($defaults = [])
}

/**
* Will use either foreign_alias or create #join_<table>.
* Will use #ref_<link>.
*
* @return string
*/
Expand All @@ -97,7 +109,16 @@ 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, 'default' => $this->default]);
// Imants: proper way would be to get actual field type of id field of related model,
// but if we try to do so here, then we end up in infinite loop :(
//$m = $this->getModel();
$this->owner->addField($this->our_field, [
'type' => 'int', //$m->getElement($m->id_field)->type,
'system' => false,
'join' => $this->join,
'default' => $this->default,
'editable' => $this->editable,
]);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Field_SQL_One.php → src/Relation_SQL_One.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* Class description?
*/
class Field_SQL_One extends Field_One
class Relation_SQL_One extends Relation_One
{
/**
* Creates expression which sub-selects a field inside related model.
Expand Down

0 comments on commit 09c76a1

Please sign in to comment.