|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Model Class |
|
5
|
|
|
* |
|
6
|
|
|
* Main/Super class for model classes |
|
7
|
|
|
* |
|
8
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
9
|
|
|
* @author Omar El Gabry <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
class Model { |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Table name for the Model. |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
public $table = false; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Array of validation errors |
|
23
|
|
|
* |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $errors = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Constructor |
|
30
|
|
|
* |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(){ |
|
33
|
|
|
if($this->table === false){ |
|
34
|
|
|
$this->table = $this->pluralize(get_class($this)); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* pluralize for table names |
|
40
|
|
|
* |
|
41
|
|
|
* Automatically selects a database table name based on a pluralized lowercase object class name |
|
42
|
|
|
* (i.e. class 'User' => table 'users') |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $word |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
private function pluralize($word){ |
|
48
|
|
|
|
|
49
|
|
|
$word = strtolower($word); |
|
50
|
|
|
$plural = [ |
|
51
|
|
|
"newsfeed" => "newsfeed", |
|
52
|
|
|
"man" => "men", |
|
53
|
|
|
"woman" => "women" |
|
54
|
|
|
]; |
|
55
|
|
|
|
|
56
|
|
|
return isset($plural[$word])? $plural[$word]: $word . "s"; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* delete record by id |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $id |
|
63
|
|
|
* @return bool |
|
64
|
|
|
* @throws Exception if feed couldn't be deleted |
|
65
|
|
|
*/ |
|
66
|
|
View Code Duplication |
public function deleteById($id){ |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
$database = Database::openConnection(); |
|
69
|
|
|
$database->deleteById($this->table, $id); |
|
70
|
|
|
|
|
71
|
|
|
if($database->countRows() !== 1){ |
|
72
|
|
|
throw new Exception ("Couldn't delete news feed"); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* get errors |
|
78
|
|
|
* |
|
79
|
|
|
* @return array errors |
|
80
|
|
|
*/ |
|
81
|
|
|
public function errors(){ |
|
82
|
|
|
return $this->errors; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* is record exists? |
|
87
|
|
|
* |
|
88
|
|
|
* Almost all methods in model needs you to pass the current user id, |
|
89
|
|
|
* Another approach is to create in Model class a property call id(will be inherited by all extending classes) |
|
90
|
|
|
* Ex: |
|
91
|
|
|
* Inside postsController: |
|
92
|
|
|
* post->id = $postId |
|
93
|
|
|
* post->updatePost(....) -> Inside updatePost() you can get the post id by: $this->id |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $id |
|
96
|
|
|
* @return bool |
|
97
|
|
|
*/ |
|
98
|
|
|
public function exists($id){ |
|
99
|
|
|
|
|
100
|
|
|
$database = Database::openConnection(); |
|
101
|
|
|
$database->getById($this->table, $id); |
|
102
|
|
|
|
|
103
|
|
|
return $database->countRows() === 1; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Counting the number of a current model's table. |
|
108
|
|
|
* |
|
109
|
|
|
* @return integer |
|
110
|
|
|
*/ |
|
111
|
|
|
public function countAll(){ |
|
112
|
|
|
|
|
113
|
|
|
$database = Database::openConnection(); |
|
114
|
|
|
return $database->countAll($this->table); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* adds parts to current query using an array. |
|
119
|
|
|
* |
|
120
|
|
|
* The array will have $key which is the field value, and the $value is the query to be added to the current query |
|
121
|
|
|
* Only fields with existing value(false, and 0 are accepted as valid values) will be considered in our query. |
|
122
|
|
|
* |
|
123
|
|
|
* @param array $options |
|
124
|
|
|
* @param string $implodeBy |
|
125
|
|
|
* @return string |
|
126
|
|
|
*/ |
|
127
|
|
|
public function applyOptions(array $options, $implodeBy){ |
|
128
|
|
|
|
|
129
|
|
|
$queries = []; |
|
130
|
|
|
|
|
131
|
|
|
foreach($options as $key => $value){ |
|
132
|
|
|
if(!empty($key) || $key === false || $key === 0){ |
|
133
|
|
|
$queries[] = $value; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
return implode($implodeBy, $queries); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.