Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
123d46d33c | |||
fbd5b350bd | |||
65c224c636 | |||
2bf938a9b7 | |||
6cd26a88ea | |||
293b5af3ff | |||
022ba575b7 | |||
643c3e714f | |||
db2864395c | |||
1cd06d5fe6 | |||
c913f65b91 | |||
244d8cc414 | |||
c8a7781c88 | |||
630c971b45 | |||
1505539e61 | |||
c441d41a02 |
@ -2,7 +2,7 @@
|
||||
namespace ProVM\Common\Alias;
|
||||
|
||||
use \Model as BaseModel;
|
||||
use ProVM\Common\Alias\Model as ModelInterface;
|
||||
use ProVM\Common\Define\Model as ModelInterface;
|
||||
use ProVM\Common\Factory\Model as ModelFactory;
|
||||
|
||||
abstract class Model extends BaseModel implements ModelInterface {
|
||||
@ -122,4 +122,35 @@ abstract class Model extends BaseModel implements ModelInterface {
|
||||
public function toArray(): array {
|
||||
return $this->asArray();
|
||||
}
|
||||
|
||||
protected static function parseInput($input): array {
|
||||
return array_intersect_key((array) $input, array_combine(static::$fields, static::$fields));
|
||||
}
|
||||
public static function add(ModelFactory $factory, $input): ?ModelInterface {
|
||||
$data = static::parseInput($input);
|
||||
$class = get_called_class();
|
||||
if (method_exists($class, 'find')) {
|
||||
$obj = static::find($factory, $input);
|
||||
} else {
|
||||
$where = $data;
|
||||
array_walk($where, function(&$item, $key) {
|
||||
$item = [$key, $item];
|
||||
});
|
||||
$where = array_values($where);
|
||||
$obj = $factory->find($class)->where($where)->one();
|
||||
}
|
||||
if ($obj === null) {
|
||||
$obj = $factory->create($class, $data);
|
||||
}
|
||||
return $obj;
|
||||
}
|
||||
public function edit($input): bool {
|
||||
$data = static::parseInput($input);
|
||||
foreach (static::$fields as $field) {
|
||||
if ($this->{$field} != $data[$field]) {
|
||||
$this->{$field} = $data[$field];
|
||||
}
|
||||
}
|
||||
return $this->save();
|
||||
}
|
||||
}
|
||||
|
@ -11,4 +11,7 @@ interface Model {
|
||||
public function siblingOf(string $sibling_model_class, string $connecting_table, array $relation_definitions): ?array;
|
||||
|
||||
public function toArray(): array;
|
||||
|
||||
public static function add(ModelFactory $factory, $input): ?Model;
|
||||
public function edit($input): bool;
|
||||
}
|
||||
|
13
common/Define/Model/Date.php
Normal file
13
common/Define/Model/Date.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Define\Model;
|
||||
|
||||
use Carbon\Carbon;
|
||||
|
||||
trait Date {
|
||||
public function date(\DateTime $date = null) {
|
||||
if ($date === null) {
|
||||
return Carbon::parse($this->date);
|
||||
}
|
||||
$this->date = $data->format('Y-m-d');
|
||||
}
|
||||
}
|
13
common/Define/Model/DateTime.php
Normal file
13
common/Define/Model/DateTime.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Define\Model;
|
||||
|
||||
use Carbon\Carbon;
|
||||
|
||||
trait DateTime {
|
||||
public function dateTime(\DateTime $date_time = null) {
|
||||
if ($date_time === null) {
|
||||
return Carbon::parse($this->date_time);
|
||||
}
|
||||
$this->date_time = $date_time->format('c');
|
||||
}
|
||||
}
|
13
common/Define/Model/Time.php
Normal file
13
common/Define/Model/Time.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Define\Model;
|
||||
|
||||
use Carbon\Carbon;
|
||||
|
||||
trait Time {
|
||||
public function time(\DateTime $time = null) {
|
||||
if ($time === null) {
|
||||
return Carbon::parse($this->time);
|
||||
}
|
||||
$this->time = $time->format('H:i:s e');
|
||||
}
|
||||
}
|
@ -3,7 +3,7 @@ namespace ProVM\Common\Factory;
|
||||
|
||||
use ORM;
|
||||
use Model as BaseModel;
|
||||
use ProVM\Common\Alias\Model as ModelInterface;
|
||||
use ProVM\Common\Define\Model as ModelInterface;
|
||||
|
||||
class Model {
|
||||
public function reset(): Model {
|
||||
@ -79,9 +79,9 @@ class Model {
|
||||
'table' => $j['table'] ?? $j[0],
|
||||
'from' => $j['from'] ?? $j[1],
|
||||
'to' => $j['to'] ?? $j[2],
|
||||
'sym' => $j['sym'] ?? ($j[3] ?? '='),
|
||||
'symb' => $j['symb'] ?? ($j[3] ?? '='),
|
||||
'alias' => $j['alias'] ?? '',
|
||||
'type' => strtolower($j['type']) ?? '',
|
||||
'type' => strtolower($j['type'] ?? ''),
|
||||
'params' => $j['params'] ?? ''
|
||||
];
|
||||
$this->joins []= $join;
|
||||
@ -97,8 +97,8 @@ class Model {
|
||||
$cond = (object) [
|
||||
'column' => $c['column'] ?? $c[0],
|
||||
'value' => $c['value'] ?? $c[1],
|
||||
'sym' => strtolower($c['sym'] ?? ($c[2] ?? '=')),
|
||||
'type' => strtolower($c['type']) ?? ''
|
||||
'symb' => strtolower($c['symb'] ?? ($c[2] ?? '=')),
|
||||
'type' => strtolower($c['type'] ?? '')
|
||||
];
|
||||
$this->conditions []= $cond;
|
||||
}
|
||||
@ -154,7 +154,7 @@ class Model {
|
||||
return $orm;
|
||||
}
|
||||
foreach ($this->columns as $col) {
|
||||
$orm = $orm->select(trim(implode('.', $col), '.'));
|
||||
$orm = $orm->select(trim(implode('.', (array) $col), '.'));
|
||||
}
|
||||
return $orm;
|
||||
}
|
||||
@ -192,7 +192,7 @@ class Model {
|
||||
}
|
||||
if ($join->type == 'raw') {
|
||||
$orm = $orm->{$method}($join->table, [$join->from, $join->symb, $join->to], $join->alias, $join->params);
|
||||
} elseif ($join->alias === '') {
|
||||
} elseif ($join->alias !== '') {
|
||||
$orm = $orm->{$method}($join->table, [$join->from, $join->symb, $join->to], $join->alias);
|
||||
} else {
|
||||
$orm = $orm->{$method}($join->table, [$join->from, $join->symb, $join->to]);
|
||||
@ -206,7 +206,7 @@ class Model {
|
||||
}
|
||||
foreach ($this->conditions as $cond) {
|
||||
$method = 'where';
|
||||
switch ($cond->sym) {
|
||||
switch ($cond->symb) {
|
||||
case '<':
|
||||
$method = 'whereLt';
|
||||
break;
|
||||
@ -363,10 +363,13 @@ class Model {
|
||||
return $results;
|
||||
}
|
||||
public function array(): ?array {
|
||||
$results = $this->build()->findArray();
|
||||
if (!$results) {
|
||||
$results = $this->many();
|
||||
if (!$results or $results === null) {
|
||||
return null;
|
||||
}
|
||||
array_walk($results, function(&$item) {
|
||||
$item = $item->toArray();
|
||||
});
|
||||
return $results;
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Form;
|
||||
|
||||
use ProVM\Common\Define\Model as BaseModel;
|
||||
use ProVM\Common\Alias\Model as BaseModel;
|
||||
|
||||
abstract class Model extends BaseModel {}
|
||||
|
Reference in New Issue
Block a user