|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpBoot\ORM; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Cache\Cache; |
|
6
|
|
|
use PhpBoot\DB\DB; |
|
7
|
|
|
|
|
8
|
|
|
class ModelWithObject |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* ModelWithObject constructor. |
|
12
|
|
|
* @param DB $db |
|
13
|
|
|
* @param object $entity |
|
14
|
|
|
* @param Cache $cache |
|
15
|
|
|
*/ |
|
16
|
4 |
|
public function __construct(DB $db, $entity, Cache $cache) |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
4 |
|
is_object($entity) or \PhpBoot\abort(new \InvalidArgumentException('object required')); |
|
|
|
|
|
|
19
|
4 |
|
$entityName = get_class($entity); |
|
20
|
4 |
|
$this->db = $db; |
|
21
|
4 |
|
$builder = $db->getApp()->get(ModelContainerBuilder::class); |
|
22
|
4 |
|
$this->entity = $builder->build($entityName); |
|
23
|
4 |
|
$this->object = $entity; |
|
24
|
4 |
|
} |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
1 |
|
public function create() |
|
28
|
|
|
{ |
|
29
|
1 |
|
$data = []; |
|
30
|
1 |
|
foreach ($this->getColumns() as $column){ |
|
31
|
1 |
|
if(isset($this->object->$column)){ |
|
32
|
1 |
|
if(is_array($this->object->$column) || is_object($this->object->$column)){ |
|
33
|
|
|
$data[$column] = json_encode($this->object->$column); |
|
34
|
|
|
}else{ |
|
35
|
1 |
|
$data[$column] = $this->object->$column; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
1 |
|
} |
|
39
|
1 |
|
} |
|
40
|
1 |
|
$id = $this->db->insertInto($this->entity->getTable()) |
|
41
|
1 |
|
->values($data) |
|
42
|
1 |
|
->exec()->lastInsertId(); |
|
43
|
1 |
|
$this->object->{$this->entity->getPK()} = $id; |
|
44
|
1 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param array $columns columns to update. if columns is empty array, update all of the columns |
|
48
|
|
|
* @return int rows updated |
|
49
|
|
|
*/ |
|
50
|
2 |
|
public function update(array $columns=[]) |
|
51
|
|
|
{ |
|
52
|
2 |
|
$data = []; |
|
53
|
2 |
|
$pk = $this->entity->getPK(); |
|
54
|
2 |
|
foreach ($this->getColumns() as $column){ |
|
55
|
2 |
|
if(count($columns)){ |
|
56
|
1 |
|
if(!in_array($column, $columns)){ |
|
57
|
1 |
|
continue; |
|
58
|
|
|
} |
|
59
|
1 |
|
} |
|
60
|
2 |
|
if($pk != $column && isset($this->object->$column)){ |
|
61
|
2 |
|
if(is_array($this->object->$column) || is_object($this->object->$column)){ |
|
62
|
|
|
$data[$column] = json_encode($this->object->$column); |
|
63
|
|
|
}else{ |
|
64
|
2 |
|
$data[$column] = $this->object->$column; |
|
65
|
|
|
} |
|
66
|
2 |
|
} |
|
67
|
2 |
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
$this->db->update($this->entity->getTable()) |
|
70
|
2 |
|
->set($data) |
|
71
|
2 |
|
->where("`{$pk}` = ?", $this->object->$pk) |
|
72
|
2 |
|
->exec()->rows; |
|
73
|
2 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return int rows deleted |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public function delete() |
|
79
|
|
|
{ |
|
80
|
1 |
|
$pk = $this->entity->getPK(); |
|
81
|
1 |
|
return $this->db->deleteFrom($this->entity->getTable()) |
|
82
|
1 |
|
->where([$pk => $this->object->$pk]) |
|
83
|
1 |
|
->exec()->rows; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
3 |
|
protected function getColumns() |
|
87
|
|
|
{ |
|
88
|
3 |
|
$columns = []; |
|
89
|
3 |
|
foreach ($this->entity->getProperties() as $p){ |
|
90
|
3 |
|
$columns[] = $p->name; |
|
91
|
3 |
|
} |
|
92
|
3 |
|
return $columns; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @var object |
|
97
|
|
|
*/ |
|
98
|
|
|
protected $object; |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @var ModelContainer |
|
102
|
|
|
*/ |
|
103
|
|
|
protected $entity; |
|
104
|
|
|
/** |
|
105
|
|
|
* @var DB |
|
106
|
|
|
*/ |
|
107
|
|
|
protected $db; |
|
108
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.