Relaciones

This commit is contained in:
2020-01-21 10:58:21 -03:00
parent b84c385bfc
commit bef1b08d0e
3 changed files with 88 additions and 1 deletions

View File

@ -7,7 +7,7 @@ use \Model as BaseModel;
use Aldarien\Common\Definition\Model as ModelInterface;
use Aldarien\Common\Factory\Model as ModelFactory;
class Model extends BaseModel implements ModelInterface {
abstract class Model extends BaseModel implements ModelInterface {
const CHILD_KEY = 'child_key';
const PARENT_KEY = 'parent_key';
const SELF_KEY = 'self_key';

54
src/RelationNode.php Normal file
View File

@ -0,0 +1,54 @@
<?php
namespace Aldarien\Models;
use Aldarien\Common\Factory\Model as ModelFactory;
use Aldarien\Common\Alias\Model;
class RelationNode {
protected $source;
protected $destination;
protected $join;
protected $relation;
public function __construct() {
$this->join = 'join';
$this->relation = '=';
}
public function setSource(string $class_name, string $key): RelationNode {
return $this->set('source', $class_name, $key);
}
public function setDestination(string $class_name, string $key): RelationNode {
return $this->set('destination', $class_name, $key);
}
public function setJoin(string $join): RelationNode {
$valid = ['join', 'inner join', 'outer join', 'left join', 'left outer join', 'right join', 'right outer join'];
if (\array_search(strtolower($join), $valid) !== false) {
$this->join = strtolower($join);
}
return $this;
}
public function setRelation(string $relation): RelationNode {
$valid = ['=', '<=', '<', '>=', '>', 'like'];
if (\array_search(strtolower($relation), $valid) !== false) {
$this->relation = $relation;
}
return $this;
}
protected function set(string $type, string $class_name, string $key): RelationNode {
$this->$type = (object) [
'class' => $class_name,
'table' => Model::getTable($class_name),
'key' => $key
];
return $this;
}
public function build(ModelFactory $factory): ModelFactory {
$source = implode('.', [$this->source->table, $this->source->key]);
$destination = implode('.', [$this->destination->table, $this->destination->key]);
$method = str_replace([' '], ['_'], strtolower($this->join));
$output = $factory->{$method}([$this->destination->table, [$destination, $source, $this->relation]]);
return $output;
}
}

33
src/Relationship.php Normal file
View File

@ -0,0 +1,33 @@
<?php
namespace Aldarien\Models;
use Aldarien\Common\Factory\Model as ModelFactory;
use Aldarien\Common\Alias\Model;
class Relationship {
protected $start;
protected $nodes;
public function __construct(string $class_name) {
$this->setStart($class_name);
}
public function setStart(string $class_name) {
$this->start = (object) [
'class' => $class_name,
'table' => Model::getTable($class_name)
];
}
public function addNode(RelationNode $node) {
if ($this->nodes === null) {
$this->nodes = [];
}
$this->nodes []= $node;
}
public function build(ModelFactory $factory): ModelFactory {
$output = $factory->find($this->start->class);
foreach ($this->nodes as $node) {
$output = $node->build($factory);
}
return $output;
}
}