forked from nigarjafar/eNGeL_Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBdocumentation.php
More file actions
343 lines (238 loc) · 9.76 KB
/
DBdocumentation.php
File metadata and controls
343 lines (238 loc) · 9.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
//__________________________________________________________________________//
//****************************** Migrations ********************************//
//__________________________________________________________________________//
// in app/database/migrations write the following code structure
<?php
class TableNameTable extends Migration{
public static function up(){
$comments= self::table("table_name"); // create table object. Table name will be comments.
$comments->id() // Add id column. Properties: unsigned int, not null , autoincrement and primary key.
->addColumn("name","varchar(255)")
->timeLog()
->softDelete()
->save();
}
public static function down(){}
}
// Call function
CommentTable::up();
//*****methods:*****//
addColumn(column_name,type)->nullable() // - allow null values
addColumn(column_name,type)->notNull() // - doesn't allow null values
addColumn(column_name,type)->primaryKey() // - add primary key constraint
addColumn(column_name,type)->unique() //- add unique constraint
addColumn(column_name,type)->autoIncrement() // - add auto_increment field
addColumn(column_name,type)->defaultValue("default") // - provide a default value for a column.
id() // Add id column. Properties: unsigned int, not null , autoincrement and primary key.
timeLog() -> add created_at and updated_at columns.
softDelete() -> add deleted_at column
//****Foreign key****//
addColumn("user_id","INT UNSIGNED")->foreignKey("users","id")
addColumn(column_name,type)->foreignKey(reference_table,reference_column)// Be careful about type of foreign key: if reference column and foreign key column type are not the same, it will cause an error. Type of ids that created by id() method is INT UNSIGNED.
//**** In app folder, add require_once('database/migrations/TableNameTable.php') to migrate.php ****//
//**** In app folder, open cmd and write php migrate.php ****//
//__________________________________________________________________________//
//****************************** Models ************************************//
//__________________________________________________________________________//
$user=$this->model('User'); // Create User model object.
$user->setTable('users'); // Set table which you want to use.
//***get&***//
$user->get() //return the results (all columns)
$user->get('colname') //return the only selected column (only 1 column)
$user->get( 'colname1','colname2',.. ) //return the only selected columnS (many columns)
//***first***//
$user->first( ) //return the only first resulr
$user->first('colname') //
$user->first( ['colname1','colname2'] ) //
$user->rawQuery('query') // send query
$user->create([ //create new row
'colname'=>'value',
'colname1'=>'value1'
]);
$user->update([ //update selected row(s)
'colname'=>'value',
'colname1'=>'value1'
])
$user->delete( ) //delete selected row(s)
//****Where****//
$user->where('id',1)->get() // where id=1
$user->where('id','<',2) //where id<2
$user->where([ //where col1<value AND col2=value AND col3 LIKE value
['col1','<','value'],
['col2','=','value'],
['col3','LIKE','value']
])->get()
$user->whereBetween($col,$from,$to)->get() //WHERE col BETWEEN $from AND $to;
$user->whereNotBetween($col,$from,$to)->get() //WHERE col NOT BETWEEN $from AND $to;
$user->whereIn($col,[$in1,$in2,...])->get() //$in is array-> WHERE $col IN ($in1,$in2...)
$user->whereNotIn($col,[$in1,$in2,...])->get() //$in is array-> WHERE $col NOT IN ($in1,$in2...)
$user->whereNull($col)->get() //WHERE $col is NULL
$user->whereNotNull($col)->get() //WHERE $col is NOT NULL
$user->whereColumn($col1, $col2) //WHERE $col1=$col2
$user->whereColumn($col1,$operand, $col2) //WHERE $col1 $operand $col2 //$operand: = < > LIKE ...
$user->whereColumn([ //WHERE `created_at`> `updated_at` AND `name` LIKE `surname /
['created_at','>','updated_at'],
['name','LIKE','surname'],
])->get()
//****orWhere****//
$user->where('id',1)->orWhere('name', 'John')->get() // id=1 OR name=John
$user->where('id',2)->orWhere('id','>',5)->get() //where id=2 OR id>5
$user->where('id',1)->orWhere([ //where id=1 OR col1<value1 OR col2 = value2 OR col3 LIKE value3
['col1','<','value1'],
['col2','=','value2'],
['col3','LIKE','value3']
])->get()
//*******//
$user->orderBy('id','asc')->get()// .... ORDER BY id ASC
$user->inRandomOrder()->get()
$user->limit( 5)->get() //.... LIMIT 5
$user->offset( 5)->get() //.... OFFSET 5
//******//
$user->count()->get() //SELECT COUNT
$user->max('id')->get() //SELECT MAX(id)
$user->min('id')->get() //SELECT MIN(id)
$user->avg('id')->get() //SELECT AVG(id)
$user->sum('id')->get() // SELECT SUM(id)
//******//
$user->distinct() // SELECT DISTINCT ...
//**Joins***//
//Inner
join($joinTable,$baseTableCol,$operator,$joinTableCol)
$user->join('companies','users.id','=','companies.user_id')
->get('users.*','companies.id') // SELECT * FROM users JOIN companies ON users.id = companies.user_id
//Left
leftJoin($joinTable,$baseTableCol,$operator,$joinTableCol)
$user->leftJoin('companies','users.id','=','companies.user_id')
->get('users.*','companies.id') // SELECT * FROM users LEFT JOIN companies ON users.id = companies.user_id
//Right
rightJoin($joinTable,$baseTableCol,$operator,$joinTableCol)
$user->rightJoin('companies','users.id','=','companies.user_id')
->get('users.*','companies.id') // SELECT * FROM users RIGHT JOIN companies ON users.id = companies.user_id
//Cross
crossJoin($joinTablel)
$user->crossJoin('companies')
->get('users.*','companies.id') // SELECT * FROM users CROSS JOIN companies
//__________________________________________________________________________//
//****************************** Soft Delete ********************************//
//__________________________________________________________________________//
$user->delete() //When soft deleting a model, it is not actually removed from your database. Instead, a deleted_at timestamp is set on the record. When querying a model that uses soft deletes, the "deleted" models will not be included in query results.
$user->forceDelete() // delete row from database
$user->recover() //recover soft deleted row
$user->withTrash() //return results with "deleted" models.
//__________________________________________________________________________//
//****************************** Model-> Relationships **********************//
//__________________________________________________________________________//
//________________________________________________//
//belongsTo//
//_______________________________________________//
belongsTo($model,$baseTableCol,$relatedTableIDCol)
belongsTo($model)
// companies "Company" ->$model
// id -> $relatedTableIDCol
// name
// posts -> "Post" ->$model
// id $relIDcol
// company_id $baseTableCol
// title
//
class Post extends Model{
public $table="posts";
public function company(){
return $this->belongsTo("Company");
//This is equal to
//return $this->belongsTo("Company","company_id","id");
}
}
//________________________________________________//
//hasOne//
//_______________________________________________//
hasOne($model,$baseTableIDCol,$relatedTableCol)
hasOne($model)
// companies "Company" ->$model
// id
// user_id $relatedTableCol
// name
//
// users ->
// id $baseTableIDCol
// name
//
class User extends Model{
public $table="users";
public function company(){
return $this->hasOne("Company");
//This is equal to
//return $this->hasMany("Company","id","user_id");
}
}
//________________________________________________//
//hasMany//
//_______________________________________________//
hasMany($model,$baseTableIDCol,$relatedTableCol)
hasMany($model)
// companies
// id -> $baseTableIDCol
// name
// posts -> "Post" ->$model
// id $relIDcol
// company_id $relatedTableCol
// title
//
class Company extends Model{
public $table="companies";
public function posts(){
return $this->hasMany("Post");
//This is equal to
//return $this->hasMany("Post","id","company_id");
}
}
//Reverse in One To Many is belongsTo as One to One.
//________________________________________________//
//belongsToMany//
//_______________________________________________//
belongsToMany($model,$pivotTable,$baseTableIDCol,$basePivotCol,$relatedTableIDCol,$relatedPivotCol)
belongsToMany($model,$pivotTable)
// tags
// id -> $baseTableIDCol
// name
// posts -> Post ->$model
// id $relatedTableIDCol
// title
// post_tag -> $pivotTable
//post_id $relatedPivotCol
//tag_id $basePivotCol
class Tag extends Model{
public $table="tags";
public function posts(){
return $this->belongsToMany("Post","post_tag");
//This is equal to
//return $this->belongsToMany("Post","post_tag","id","tag_id","id","post_id");
}
}
//________________________________________________//
//Has Many Through//
//_______________________________________________//
hasManyThrough($throughModel, $resultModel,$baseTableIDcol,$throughTableCol,$throughTableIDcol,$resultTableCol)
hasManyThrough($throughModel, $resultModel)
// countries
// id ->$baseTableIDcol
// name
// users // User.php ->$throughModel
// id -> $throughTableIDcol
// country_id ->$throughTableCol
// name
// posts // Post.php ->$resultModel
// id
// user_id ->$resultTableCol
// title
class Country extends Model{
public $table="countries";
public function posts(){
return $this->hasManyThrough("Company","Post");
// Or return $this->hasManyThrough("Company","Post","id","country_id","id","company_id");
}
}
//In order to use less parameters in functions column in db names should be as follow
// id (every table must have "id")
// user_id,company_id ... (Not userId or smt else) (in dependent tables)