|
1
|
|
|
<?php |
|
2
|
|
|
namespace PhpBoot\ORM; |
|
3
|
|
|
|
|
4
|
|
|
use Doctrine\Common\Cache\Cache; |
|
5
|
|
|
use PhpBoot\DB\DB; |
|
6
|
|
|
|
|
7
|
|
|
class ModelWithClass |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Model constructor. |
|
11
|
|
|
* @param DB $db |
|
12
|
|
|
* @param string $entityName |
|
13
|
|
|
* @param Cache $cache |
|
14
|
|
|
*/ |
|
15
|
6 |
|
public function __construct(DB $db, $entityName, Cache $cache) |
|
|
|
|
|
|
16
|
|
|
{ |
|
17
|
6 |
|
$this->db = $db; |
|
18
|
6 |
|
$builder = $db->getApp()->get(ModelContainerBuilder::class); |
|
19
|
6 |
|
$this->entity = $builder->build($entityName); |
|
20
|
6 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param string $id |
|
24
|
|
|
* @return mixed|null |
|
25
|
|
|
*/ |
|
26
|
1 |
|
public function find($id) |
|
27
|
|
|
{ |
|
28
|
1 |
|
$row = $this->db->select($this->getColumns()) |
|
|
|
|
|
|
29
|
1 |
|
->from($this->entity->getTable()) |
|
30
|
1 |
|
->where("`{$this->entity->getPK()}` = ?", $id) |
|
31
|
1 |
|
->getFirst(); |
|
32
|
1 |
|
if($row){ |
|
33
|
|
|
return $this->entity->make($row, false); |
|
34
|
|
|
}else{ |
|
35
|
1 |
|
return null; |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return int rows deleted |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public function delete($id) |
|
43
|
|
|
{ |
|
44
|
1 |
|
return $this->db->deleteFrom($this->entity->getTable()) |
|
45
|
1 |
|
->where([$this->entity->getPK()=>$id]) |
|
46
|
1 |
|
->limit(1) |
|
47
|
1 |
|
->exec()->rows; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* where 语法见 @see WhereRule |
|
52
|
|
|
* @param array|string $expr |
|
|
|
|
|
|
53
|
|
|
* @param mixed|null $_ |
|
54
|
|
|
* @return \PhpBoot\DB\rules\basic\OrderByRule |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function deleteWhere($conditions, $_=null) |
|
|
|
|
|
|
57
|
|
|
{ |
|
58
|
1 |
|
$query = $this->db->deleteFrom($this->entity->getTable()); |
|
59
|
1 |
|
return call_user_func_array([$query, 'where'], func_get_args()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return false|int |
|
64
|
|
|
*/ |
|
65
|
|
|
public function count() |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->db->select($this->getColumns()) |
|
68
|
|
|
->from($this->entity->getTable()) |
|
69
|
|
|
->count(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* where 语法见 @see WhereRule |
|
74
|
|
|
* @param array|string|null $conditions |
|
75
|
|
|
* @param string $_ |
|
76
|
|
|
* @return \PhpBoot\DB\rules\select\GroupByRule |
|
77
|
|
|
*/ |
|
78
|
2 |
|
public function findWhere($conditions=null, $_=null) |
|
|
|
|
|
|
79
|
|
|
{ |
|
80
|
2 |
|
$query = $this->db->select($this->getColumns()) |
|
81
|
2 |
|
->from($this->entity->getTable()); |
|
82
|
|
|
$query->context->resultHandler = function ($result){ |
|
83
|
|
|
foreach ($result as &$i){ |
|
84
|
|
|
$i = $this->entity->make($i, false); |
|
85
|
|
|
} |
|
86
|
|
|
return $result; |
|
87
|
|
|
}; |
|
88
|
2 |
|
return call_user_func_array([$query, 'where'], func_get_args()); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* where 语法见 @see WhereRule |
|
93
|
|
|
* @param array|string $expr |
|
94
|
|
|
* @param mixed|null $_ |
|
95
|
|
|
* @return \PhpBoot\DB\rules\basic\WhereRule |
|
96
|
|
|
*/ |
|
97
|
1 |
|
public function update($expr, $_=null) |
|
|
|
|
|
|
98
|
|
|
{ |
|
99
|
1 |
|
$query = $this->db->update($this->entity->getTable()); |
|
100
|
1 |
|
return call_user_func_array([$query, 'set'], func_get_args()); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
3 |
|
protected function getColumns() |
|
104
|
|
|
{ |
|
105
|
3 |
|
$columns = []; |
|
106
|
3 |
|
foreach ($this->entity->getProperties() as $p){ |
|
107
|
3 |
|
$columns[] = $p->name; |
|
108
|
3 |
|
} |
|
109
|
3 |
|
return $columns; |
|
110
|
|
|
} |
|
111
|
|
|
/** |
|
112
|
|
|
* @var ModelContainer |
|
113
|
|
|
*/ |
|
114
|
|
|
protected $entity; |
|
115
|
|
|
/** |
|
116
|
|
|
* @var DB |
|
117
|
|
|
*/ |
|
118
|
|
|
protected $db; |
|
119
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.