Compare commits
20 Commits
Author | SHA1 | Date | |
---|---|---|---|
7c727d93e9 | |||
a8d548c0c4 | |||
e02b8c4063 | |||
f03df583d7 | |||
89d1db7a7e | |||
17453427a2 | |||
8dc0a27fd9 | |||
5b1a61cd3b | |||
ae172b902c | |||
d3cb68c5ca | |||
7f81b987c9 | |||
0a46604e0c | |||
af801e769f | |||
e9bee7fa48 | |||
c40baaad3f | |||
ac019aac3f | |||
a82fdce64b | |||
86ffb0b84c | |||
8126b1f67d | |||
7899b9f6c8 |
@ -3,6 +3,7 @@ namespace ProVM\Common\Define;
|
||||
|
||||
use \Model as BaseModel;
|
||||
use ProVM\Common\Alias\Model as ModelInterface;
|
||||
use ProVM\Common\Factory\Model as ModelFactory;
|
||||
|
||||
abstract class Model extends BaseModel implements ModelInterface {
|
||||
const SELF_KEY = 'self_key';
|
||||
@ -12,7 +13,7 @@ abstract class Model extends BaseModel implements ModelInterface {
|
||||
const SELF_CONNECT_KEY = 'self_connect_key';
|
||||
const SIBLING_CONNECT_KEY = 'sibling_connect_key';
|
||||
|
||||
public function getTable() {
|
||||
public function getTable(): string {
|
||||
return static::$_table;
|
||||
}
|
||||
|
||||
@ -60,10 +61,10 @@ abstract class Model extends BaseModel implements ModelInterface {
|
||||
], [
|
||||
Model::PARENT_KEY => 'id'
|
||||
]);
|
||||
$parent_table = (new $parent_class())->getTable();
|
||||
$parent_table = (new $parent_model_class())->getTable();
|
||||
return $this->factory
|
||||
->find($parent_model_class)
|
||||
->wherer([
|
||||
->where([
|
||||
[
|
||||
$relation_definitions[Model::PARENT_KEY],
|
||||
$this->{$relation_definitions[Model::SELF_KEY]}
|
||||
@ -81,7 +82,7 @@ abstract class Model extends BaseModel implements ModelInterface {
|
||||
Model::SELF_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)
|
||||
->select([
|
||||
[
|
||||
|
@ -22,11 +22,25 @@ class Model {
|
||||
}
|
||||
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 !== false) {
|
||||
return $model;
|
||||
}
|
||||
}
|
||||
return BaseModel::factory($model_class)->create($data);
|
||||
}
|
||||
protected $class;
|
||||
public function find(string $model_class): Model {
|
||||
if (!class_exists($model_class)) {
|
||||
throw new \InvalidArgumentException('El modelo ' . $model_class . ' no existe.');
|
||||
}
|
||||
$this->reset();
|
||||
$this->class = $model_class;
|
||||
return $this;
|
||||
}
|
||||
@ -79,14 +93,14 @@ class Model {
|
||||
}
|
||||
foreach ($conditions as $c) {
|
||||
$cond = (object) [
|
||||
'column' => $j['column'] ?? $j[0],
|
||||
'value' => $j['value'] ?? $j[1],
|
||||
'sym' => strtolower($j['sym'] ?? ($j[2] ?? '=')),
|
||||
'type' => strtolower($j['type']) ?? ''
|
||||
'column' => $c['column'] ?? $c[0],
|
||||
'value' => $c['value'] ?? $c[1],
|
||||
'sym' => strtolower($c['sym'] ?? ($c[2] ?? '=')),
|
||||
'type' => strtolower($c['type']) ?? ''
|
||||
];
|
||||
$this->conditions []= $cond;
|
||||
}
|
||||
return $this->conditions;
|
||||
return $this;
|
||||
}
|
||||
protected $grouping;
|
||||
public function group($groups): Model {
|
||||
@ -328,13 +342,19 @@ class Model {
|
||||
return $orm->offset($this->offset);
|
||||
}
|
||||
|
||||
public function one(): ModelInterface {
|
||||
$result = $this->build()->findOne();
|
||||
public function one($id = null): ModelInterface {
|
||||
$result = $this->build()->findOne($id);
|
||||
if (!$result) {
|
||||
return false;
|
||||
}
|
||||
$result->setFactory($this);
|
||||
return $result;
|
||||
}
|
||||
public function many(): array {
|
||||
$results = $this->build()->findMany();
|
||||
if (!$results) {
|
||||
return false;
|
||||
}
|
||||
foreach ($results as &$r) {
|
||||
$r->setFactory($this);
|
||||
}
|
||||
@ -342,6 +362,9 @@ class Model {
|
||||
}
|
||||
public function array(): array {
|
||||
$results = $this->build()->findArray();
|
||||
if (!$results) {
|
||||
return false;
|
||||
}
|
||||
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