1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Model class |
5
|
|
|
* |
6
|
|
|
* Base class for an ORM. |
7
|
|
|
* |
8
|
|
|
* @package core |
9
|
|
|
* @author [email protected] |
10
|
|
|
* @copyright Caffeina srl - 2015 - http://caffeina.it |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
abstract class Model implements JsonSerializable { |
14
|
|
|
use Module, Persistence, Events, Relation; |
15
|
|
|
|
16
|
|
|
public static function where($where_sql = false, $params = [], $flush = false){ |
|
|
|
|
17
|
|
|
$key = static::persistenceOptions('key'); |
18
|
|
|
|
19
|
|
|
return SQL::reduce("SELECT {$key} FROM " . static::persistenceOptions('table') . ($where_sql ? " where {$where_sql}" : ''), $params, function($results, $row) use ($key) { |
20
|
|
|
$results[] = static::load($row->{$key}); |
21
|
|
|
return $results; |
22
|
|
|
}, []); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public static function count($where_sql = false, $params = []) { |
26
|
|
|
return (int) SQL::value('SELECT COUNT(1) FROM ' . static::persistenceOptions('table') . ($where_sql ? " where {$where_sql}" : ''), $params); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public static function all($page=1, $limit=-1){ |
30
|
|
|
return static::where($limit < 1 ? "" : "1 limit {$limit} offset " . (max(1,$page)-1)*$limit); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function primaryKey(){ |
34
|
|
|
$key = static::persistenceOptions('key'); |
35
|
|
|
return $this->{$key}; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function create($data){ |
39
|
|
|
$tmp = new static; |
40
|
|
|
$data = (object)$data; |
41
|
|
|
foreach (array_keys(get_object_vars($tmp)) as $key) { |
42
|
|
|
if (isset($data->{$key})) $tmp->{$key} = $data->{$key}; |
43
|
|
|
} |
44
|
|
|
$tmp->save(); |
45
|
|
|
static::trigger('create',$tmp,$data); |
46
|
|
|
return $tmp; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function export($transformer=null, $disabled_relations=[]){ |
50
|
|
|
$data = []; |
51
|
|
|
if (!is_callable($transformer)) $transformer = function($k,$v){ return [$k=>$v]; }; |
52
|
|
|
foreach (get_object_vars($this) as $key => $value) { |
53
|
|
View Code Duplication |
if ($res = $transformer($key, $value)){ |
54
|
|
|
$data[key($res)] = current($res); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
foreach (static::relationOptions()->links as $hash => $link) { |
59
|
|
|
$relation = $link->method; |
60
|
|
|
// Expand relations but protect from self-references loop |
61
|
|
|
if (isset($disabled_relations[$hash])) continue; |
62
|
|
|
$disabled_relations[$hash] = true; |
63
|
|
|
$value = $this->$relation; |
64
|
|
|
if ($value && is_array($value)) |
65
|
|
|
foreach ($value as &$val) $val = $val->export(null,$disabled_relations); |
66
|
|
|
else |
67
|
|
|
$value = $value ? $value->export(null,$disabled_relations) : false; |
68
|
|
|
unset($disabled_relations[$hash]); |
69
|
|
|
|
70
|
|
View Code Duplication |
if ($res = $transformer($relation, $value)){ |
71
|
|
|
$data[key($res)] = current($res); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
return $data; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function jsonSerialize() { |
79
|
|
|
return $this->export(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.