Compare commits
20 Commits
Author | SHA1 | Date | |
---|---|---|---|
9dc71e4d77 | |||
65bec43b45 | |||
c6806a1c62 | |||
5f3f6b72e5 | |||
6fd19a11be | |||
a1466143b5 | |||
7c727d93e9 | |||
a8d548c0c4 | |||
e02b8c4063 | |||
f03df583d7 | |||
89d1db7a7e | |||
17453427a2 | |||
8dc0a27fd9 | |||
5b1a61cd3b | |||
ae172b902c | |||
d3cb68c5ca | |||
7f81b987c9 | |||
0a46604e0c | |||
af801e769f | |||
e9bee7fa48 |
@ -37,7 +37,7 @@ abstract class Model extends BaseModel implements ModelInterface {
|
|||||||
}
|
}
|
||||||
return $definitions;
|
return $definitions;
|
||||||
}
|
}
|
||||||
public function parentOf(string $child_model_class, array $relation_definitions): array {
|
public function parentOf(string $child_model_class, array $relation_definitions): ?array {
|
||||||
$relation_definitions = $this->checkDefinitions($relation_definitions, [
|
$relation_definitions = $this->checkDefinitions($relation_definitions, [
|
||||||
Model::SELF_KEY,
|
Model::SELF_KEY,
|
||||||
Model::CHILD_KEY
|
Model::CHILD_KEY
|
||||||
@ -54,17 +54,17 @@ abstract class Model extends BaseModel implements ModelInterface {
|
|||||||
])
|
])
|
||||||
->many();
|
->many();
|
||||||
}
|
}
|
||||||
public function childOf(string $parent_model_class, array $relation_definitions): ModelInterface {
|
public function childOf(string $parent_model_class, array $relation_definitions): ?ModelInterface {
|
||||||
$relation_definitions = $this->checkDefinitions($relation_definitions, [
|
$relation_definitions = $this->checkDefinitions($relation_definitions, [
|
||||||
Model::SELF_KEY,
|
Model::SELF_KEY,
|
||||||
Model::PARENT_KEY
|
Model::PARENT_KEY
|
||||||
], [
|
], [
|
||||||
Model::PARENT_KEY => 'id'
|
Model::PARENT_KEY => 'id'
|
||||||
]);
|
]);
|
||||||
$parent_table = (new $parent_class())->getTable();
|
$parent_table = (new $parent_model_class())->getTable();
|
||||||
return $this->factory
|
return $this->factory
|
||||||
->find($parent_model_class)
|
->find($parent_model_class)
|
||||||
->wherer([
|
->where([
|
||||||
[
|
[
|
||||||
$relation_definitions[Model::PARENT_KEY],
|
$relation_definitions[Model::PARENT_KEY],
|
||||||
$this->{$relation_definitions[Model::SELF_KEY]}
|
$this->{$relation_definitions[Model::SELF_KEY]}
|
||||||
@ -72,7 +72,7 @@ abstract class Model extends BaseModel implements ModelInterface {
|
|||||||
])
|
])
|
||||||
->one();
|
->one();
|
||||||
}
|
}
|
||||||
public function siblingOf(string $sibling_model_class, string $connecting_table, array $relation_definitions): array {
|
public function siblingOf(string $sibling_model_class, string $connecting_table, array $relation_definitions): ?array {
|
||||||
$relation_definitions = $this->checkDefinitions($relation_definitions, [
|
$relation_definitions = $this->checkDefinitions($relation_definitions, [
|
||||||
Model::SELF_KEY,
|
Model::SELF_KEY,
|
||||||
Model::SIBLING_KEY,
|
Model::SIBLING_KEY,
|
||||||
@ -82,7 +82,7 @@ abstract class Model extends BaseModel implements ModelInterface {
|
|||||||
Model::SELF_KEY => 'id',
|
Model::SELF_KEY => 'id',
|
||||||
Model::SIBLING_KEY => 'id'
|
Model::SIBLING_KEY => 'id'
|
||||||
]);
|
]);
|
||||||
$sibling_table = (new $sibling_class())->getTable();
|
$sibling_table = (new $sibling_model_class())->getTable();
|
||||||
return $this->find($sibling_model_class)
|
return $this->find($sibling_model_class)
|
||||||
->select([
|
->select([
|
||||||
[
|
[
|
||||||
|
@ -22,11 +22,25 @@ class Model {
|
|||||||
}
|
}
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
public function create(string $model_class, array $data = null): ModelInterface {
|
||||||
|
if ($data !== null) {
|
||||||
|
$model = $this->find($model_class);
|
||||||
|
foreach ($data as $f => $v) {
|
||||||
|
$model = $model->where([[$f, $v]]);
|
||||||
|
}
|
||||||
|
$model = $model->one();
|
||||||
|
if ($model !== null) {
|
||||||
|
return $model;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return BaseModel::factory($model_class)->create($data);
|
||||||
|
}
|
||||||
protected $class;
|
protected $class;
|
||||||
public function find(string $model_class): Model {
|
public function find(string $model_class): Model {
|
||||||
if (!class_exists($model_class)) {
|
if (!class_exists($model_class)) {
|
||||||
throw new \InvalidArgumentException('El modelo ' . $model_class . ' no existe.');
|
throw new \InvalidArgumentException('El modelo ' . $model_class . ' no existe.');
|
||||||
}
|
}
|
||||||
|
$this->reset();
|
||||||
$this->class = $model_class;
|
$this->class = $model_class;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -79,10 +93,10 @@ class Model {
|
|||||||
}
|
}
|
||||||
foreach ($conditions as $c) {
|
foreach ($conditions as $c) {
|
||||||
$cond = (object) [
|
$cond = (object) [
|
||||||
'column' => $j['column'] ?? $j[0],
|
'column' => $c['column'] ?? $c[0],
|
||||||
'value' => $j['value'] ?? $j[1],
|
'value' => $c['value'] ?? $c[1],
|
||||||
'sym' => strtolower($j['sym'] ?? ($j[2] ?? '=')),
|
'sym' => strtolower($c['sym'] ?? ($c[2] ?? '=')),
|
||||||
'type' => strtolower($j['type']) ?? ''
|
'type' => strtolower($c['type']) ?? ''
|
||||||
];
|
];
|
||||||
$this->conditions []= $cond;
|
$this->conditions []= $cond;
|
||||||
}
|
}
|
||||||
@ -328,20 +342,29 @@ class Model {
|
|||||||
return $orm->offset($this->offset);
|
return $orm->offset($this->offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function one(): ModelInterface {
|
public function one($id = null): ?ModelInterface {
|
||||||
$result = $this->build()->findOne();
|
$result = $this->build()->findOne($id);
|
||||||
|
if (!$result) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
$result->setFactory($this);
|
$result->setFactory($this);
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
public function many(): array {
|
public function many(): ?array {
|
||||||
$results = $this->build()->findMany();
|
$results = $this->build()->findMany();
|
||||||
|
if (!$results) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
foreach ($results as &$r) {
|
foreach ($results as &$r) {
|
||||||
$r->setFactory($this);
|
$r->setFactory($this);
|
||||||
}
|
}
|
||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
public function array(): array {
|
public function array(): ?array {
|
||||||
$results = $this->build()->findArray();
|
$results = $this->build()->findArray();
|
||||||
|
if (!$results) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
6
common/Form/Model.php
Normal file
6
common/Form/Model.php
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?php
|
||||||
|
namespace ProVM\Common\Form;
|
||||||
|
|
||||||
|
use ProVM\Common\Define\Model as BaseModel;
|
||||||
|
|
||||||
|
abstract class Model extends BaseModel {}
|
Reference in New Issue
Block a user