2
2
3
3
namespace App \Models ;
4
4
5
+ use Closure ;
5
6
use Database \DBConnection ;
7
+ use Exception ;
6
8
use PDO ;
7
9
8
10
class Model
9
11
{
10
12
11
13
protected static $ db ;
12
- static protected $ table ;
14
+ protected static $ table ;
15
+ protected static $ primaryKey = "id " ;
13
16
protected static $ sql ;
17
+ protected static $ params = [];
14
18
15
19
public function __construct ()
16
20
{
17
- self ::$ db = new DBConnection (DB , DB_NAME , DB_HOST , DB_USER , DB_PWD );
21
+ static ::$ db = new DBConnection (DB , DB_NAME , DB_HOST , DB_USER , DB_PWD );
22
+ }
23
+
24
+ public static function getInstance ()
25
+ {
26
+ return new static ();
18
27
}
19
28
20
29
// select all data tables
21
- public static function all (): array
30
+ public static function all (array $ columns = [ " * " ] ): array
22
31
{
23
- self ::$ sql = "SELECT * FROM " . static ::$ table ;
24
- return self ::query ();
32
+ $ columns = implode (', ' , $ columns );
33
+ static ::$ sql = "SELECT $ columns FROM " . static ::$ table ;
34
+ return static ::query ();
25
35
}
26
36
27
- public static function lastId ()
37
+ public static function lastInsert ()
28
38
{
29
- return static ::$ db ->getPDO ()->lastInsertId ();
39
+ $ id = static ::$ db ->getPDO ()->lastInsertId ();
40
+ return static ::findOrFail ($ id );
30
41
}
31
42
32
43
// select a row of table using id field
33
- public static function find (int $ id )
44
+ public static function find ($ id, array $ columns = [ ' * ' ] )
34
45
{
35
- self ::$ sql = "SELECT * FROM " . static ::$ table . " WHERE id = ? " ;
36
- return self ::query ([$ id ], true );
46
+ $ columns = implode (", " , $ columns );
47
+ static ::$ sql = "SELECT $ columns FROM " . static ::$ table . " WHERE " . static ::$ primaryKey . " = ? " ;
48
+ static ::$ params = [$ id ];
49
+ return static ::query (true );
37
50
}
38
51
52
+ public static function findOrFail (int $ id , array $ columns = ['* ' ])
53
+ {
54
+ $ result = static ::find ($ id , $ columns );
55
+ if (is_null ($ result )) {
56
+ throw new Exception ("Model " . static ::class . " not fount " );
57
+ }
58
+ return $ result ;
59
+ }
60
+
61
+
39
62
// insert an instance in database
40
63
public static function create (array $ data )
41
64
{
@@ -48,43 +71,83 @@ public static function create(array $data)
48
71
$ values .= ": {$ key }{$ comma }" ; // set their value
49
72
$ i ++;
50
73
}
51
- self ::$ sql = "INSERT INTO " . static ::$ table . " ( $ fields) VALUES ( $ values) " ;
74
+ static ::$ sql = "INSERT INTO " . static ::$ table . " ( $ fields) VALUES ( $ values) " ;
75
+ static ::$ params = $ data ;
76
+ static ::query (true );
77
+ return static ::lastInsert ();
78
+ }
79
+
80
+ public function first (array $ columns = ['* ' ])
81
+ {
82
+ return static ::query (true );
83
+ }
84
+
85
+ public function firstOrFail (array $ columns = ['* ' ])
86
+ {
87
+ $ result = $ this ->first ($ columns );
88
+ if (empty ($ result )) {
89
+ throw new Exception ("Model " . static ::class . " not fount " );
90
+ }
91
+ return $ result ;
92
+ }
52
93
53
- return self ::query ($ data );
94
+ public function get (array $ columns = ['* ' ]): array
95
+ {
96
+ return static ::query ();
54
97
}
55
98
56
99
public function oderBy (string $ column , string $ dir = "asc " )
57
100
{
58
- self ::$ sql .= " ORDER BY $ column $ dir " ;
59
- return self ::query ();
101
+ static ::$ sql .= " ORDER BY $ column $ dir " ;
102
+ return static ::query ();
60
103
}
61
104
62
- public static function __callStatic ( string $ name , array $ parameters )
105
+ public static function firstOrCreate ( array $ attributes = [] , array $ values = [] )
63
106
{
64
- $ name = "static_ " . $ name ;
65
- return call_user_func_array ($ name , $ parameters );
107
+ $ result = static ::where ($ attributes )->first ();
108
+ if (is_null ($ result )) {
109
+ $ result = static ::create (array_merge ($ attributes , $ values ));
110
+ }
111
+ return $ result ;
66
112
}
67
113
68
- public function update ( $ data )
114
+ public static function updateOrCreate ( array $ attributes = [], array $ values = [] )
69
115
{
70
- $ sqlRequestPart = "" ;
116
+ $ result = static ::where ($ attributes )->first ();
117
+ if ($ result ) {
118
+ $ result = $ result ->update ($ values );
119
+ } else {
120
+ $ result = static ::create (array_merge ($ attributes , $ values ));
121
+ }
122
+ return $ result ;
123
+ }
71
124
72
- $ i = 1 ;
73
- foreach ( $ data as $ key => $ value ) {
74
- $ comma = $ i === count ( $ data ) ? "" : " , " ;
75
- $ sqlRequestPart .= "{ $ key } = : { $ key }{ $ comma }" ;
76
- $ i ++ ;
125
+ public function firstOr ( $ columns = [ ' * ' ], Closure $ callback = null )
126
+ {
127
+ $ result = $ this -> first () ;
128
+ if ( $ result ) {
129
+ return $ result ;
77
130
}
78
- $ data ['id ' ] = $ this ->id ;
131
+ if (is_callable ($ columns )) {
132
+ return call_user_func_array ($ columns , []);
133
+ }
134
+ return call_user_func_array ($ callback , []);
135
+ }
79
136
80
- self ::$ sql = "UPDATE " . static ::$ table . " SET {$ sqlRequestPart } WHERE id = :id " ;
81
- return self ::query ($ data , true );
137
+ public static function destroy ($ ids ): bool
138
+ {
139
+ if (is_array ($ ids )) {
140
+ $ ids = implode (", " , $ ids );
141
+ static ::$ sql = "DELETE FROM " . static ::$ table . " WHERE " . static ::$ primaryKey . " IN ( $ ids) " ;
142
+ return static ::query ();
143
+ }
144
+ static ::$ sql = "DELETE FROM " . static ::$ table . " WHERE " . static ::$ primaryKey . " = ? " ;
145
+ static ::$ params = [$ ids ];
146
+ return static ::query ();
82
147
}
83
148
84
- // update row in database
85
- public static function static_update (int $ id , array $ data )
149
+ public function update (array $ data ): bool
86
150
{
87
- // create a part of sql for fields that we want to update
88
151
$ sqlRequestPart = "" ;
89
152
90
153
$ i = 1 ;
@@ -93,65 +156,93 @@ public static function static_update(int $id, array $data)
93
156
$ sqlRequestPart .= "{$ key } = : {$ key }{$ comma }" ;
94
157
$ i ++;
95
158
}
96
- $ data ['id ' ] = $ id ;
159
+ $ id = static ::$ primaryKey ;
160
+ $ data [$ id ] = $ this ->$ id ;
97
161
98
- self ::$ sql = "UPDATE " . static ::$ table . " SET {$ sqlRequestPart } WHERE id = :id " ;
99
- return self ::query ($ data , true );
162
+ static ::$ sql = "UPDATE " . static ::$ table . " SET {$ sqlRequestPart } WHERE " . static ::$ primaryKey . " = : " . static ::$ primaryKey ;
163
+ static ::$ params = $ data ;
164
+ return static ::query (true );
100
165
}
101
166
102
- public function delete ()
167
+ public function delete (): bool
103
168
{
104
- self ::$ sql = "DELETE FROM " . static ::$ table . " WHERE id = ? " ;
105
- return self ::query ([$ this ->id ]);
169
+ static ::$ sql = "DELETE FROM " . static ::$ table . " WHERE " . static ::$ primaryKey . " = ? " ;
170
+ $ id = static ::$ primaryKey ;
171
+ static ::$ params = [$ this ->$ id ];
172
+ static ::query ();
173
+ return true ;
106
174
}
107
- // delete a row in database using id
108
- public static function static_delete (int $ id ): bool
175
+
176
+
177
+ public static function where ($ column , $ operator = null , $ value = null )
109
178
{
110
- self ::$ sql = "DELETE FROM " . static ::$ table . " WHERE id = ? " ;
111
- return self ::query ([$ id ]);
179
+ $ wheres = "" ;
180
+
181
+ if (is_array ($ column )) {
182
+ $ i = 1 ;
183
+ foreach ($ column as $ key => $ value ) {
184
+ $ comma = $ i === count ($ column ) ? "" : " AND " ;
185
+ $ wheres .= "{$ key } = : {$ key }{$ comma }" ;
186
+ $ i ++;
187
+ }
188
+ } else {
189
+ $ wheres = "$ column $ operator ? " ;
190
+ }
191
+
192
+ $ model = static ::getInstance ();
193
+
194
+ $ model ::$ sql = "SELECT * FROM " . static ::$ table . " WHERE {$ wheres }" ;
195
+ $ model ::$ params = is_array ($ column ) ? $ column : [$ value ];
196
+ // var_dump($model::$sql);
197
+ return $ model ;
112
198
}
113
199
114
- public static function where ( array $ params )
200
+ public function orWhere ( $ column , $ operator = null , $ value = null )
115
201
{
116
202
$ wheres = "" ;
117
203
118
- $ i = 1 ;
119
- foreach ($ params as $ key => $ value ) {
120
- $ comma = $ i === count ($ params ) ? "" : " AND " ;
121
- $ wheres .= "{$ key } = : {$ key }{$ comma }" ;
122
- $ i ++;
204
+ if (is_array ($ column )) {
205
+ $ i = 1 ;
206
+ foreach ($ column as $ key => $ value ) {
207
+ $ comma = $ i === count ($ column ) ? "" : " OR " ;
208
+ $ wheres .= "{$ key } = : {$ key }{$ comma }" ;
209
+ $ i ++;
210
+ }
211
+ } else {
212
+ $ wheres .= " OR $ column $ operator $ value " ;
123
213
}
124
214
125
- self ::$ sql = "SELECT * FROM " . static :: $ table . " WHERE {$ wheres }" ;
126
- return self :: query ( $ params ) ;
215
+ static ::$ sql . = "{$ wheres }" ;
216
+ return $ this ;
127
217
}
128
218
129
219
/**
130
220
* Set query for do something in database
131
221
*
132
- * @param array|null $params If we want to make prepared request
133
222
* @param bool $single For retrieve one instance
134
223
*/
135
- private static function query (? array $ params = null , bool $ single = null )
224
+ private static function query (bool $ single = null )
136
225
{
226
+ $ params = static ::$ params ;
227
+ static ::$ db = static ::$ db ?? new DBConnection (DB , DB_NAME , DB_HOST , DB_USER , DB_PWD );
137
228
$ method = is_null ($ params ) ? 'query ' : 'prepare ' ;
138
229
$ fetch = is_null ($ single ) ? 'fetchAll ' : 'fetch ' ;
139
- self ::$ db = new DBConnection (DB , DB_NAME , DB_HOST , DB_USER , DB_PWD );
140
230
141
- if (strpos (self ::$ sql , 'INSERT ' ) === 0 || strpos (self ::$ sql , 'UPDATE ' ) === 0 || strpos (self ::$ sql , 'DELETE ' ) === 0 ) {
142
- $ stmt = self ::$ db ->getPDO ()->$ method (self ::$ sql );
143
- $ stmt ->setFetchMode (PDO ::FETCH_CLASS , self ::class, [self ::$ db ]);
231
+ if (strpos (static ::$ sql , 'INSERT ' ) === 0 || strpos (static ::$ sql , 'UPDATE ' ) === 0 || strpos (static ::$ sql , 'DELETE ' ) === 0 ) {
232
+ $ stmt = static ::$ db ->getPDO ()->$ method (static ::$ sql );
233
+ $ stmt ->setFetchMode (PDO ::FETCH_CLASS , static ::class, [static ::$ db ]);
144
234
return $ stmt ->execute ($ params );
145
235
}
146
236
147
- $ stmt = self ::$ db ->getPDO ()->$ method (self ::$ sql );
148
- $ stmt ->setFetchMode (PDO ::FETCH_CLASS , self ::class, [self ::$ db ]);
237
+ $ stmt = static ::$ db ->getPDO ()->$ method (static ::$ sql );
238
+ $ stmt ->setFetchMode (PDO ::FETCH_CLASS , static ::class, [static ::$ db ]);
149
239
150
240
if ($ method === 'query ' ) {
151
- return $ stmt ->$ fetch ();
241
+ $ results = $ stmt ->$ fetch ();
152
242
} else {
153
243
$ stmt ->execute ($ params );
154
- return $ stmt ->$ fetch ();
244
+ $ results = $ stmt ->$ fetch ();
155
245
}
246
+ return $ results ;
156
247
}
157
248
}
0 commit comments