-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathorm.php
More file actions
722 lines (558 loc) · 21.4 KB
/
orm.php
File metadata and controls
722 lines (558 loc) · 21.4 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
<?php
define('DB_HOST', 'localhost');
define('DB_NAME', 'veritabani');
define('DB_CHARSET', 'utf8mb4');
define('DB_USER', 'root');
define('DB_PASS', '');
class Orm {
protected static $pdo = null;
protected $db;
protected $tablo;
protected $primaryKey = 'id';
protected $fillable = []; // sadece bunlar set edilir
protected $guarded = ['id']; // bunlar asla set edilmez (öncelikli)
protected $wheres = [];
protected $params = [];
protected $order = '';
protected $limit = '';
protected $offset = '';
protected $with = [];
protected $joins = [];
protected $select = '*';
protected $groupBy = '';
protected $having = '';
protected $distinct = false;
protected $timestamps = true; // modeller isterse kapatabilir
protected $createdAtColumn = 'created_at';
protected $updatedAtColumn = 'updated_at';
protected $softDelete = false; // model bazlı aktif/pasif
protected $deletedAtColumn = 'deleted_at';
protected $rules = [];
protected $errors = [];
protected $debug = false;
protected $queryLog = [];
public function __construct($tablo) {
if (!self::$pdo) {
$dsn = 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';charset=' . DB_CHARSET;
try {
self::$pdo = new PDO($dsn, DB_USER, DB_PASS, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
]);
} catch (PDOException $e) {
die("Veritabanı bağlantı hatası: " . $e->getMessage());
}
}
$this->db = self::$pdo;
$this->tablo = $tablo;
}
public function enableDebug(bool $durum = true) {
if ($durum === null) {
$durum = (defined('APP_DEBUG') && APP_DEBUG === true);
}
$this->debug = $durum;
return $this;
}
protected function logQuery(string $sql, array $params = []) {
if ($this->debug) {
$this->queryLog[] = [
'query' => $sql,
'params' => $params,
'time' => date('Y-m-d H:i:s'),
];
}
}
protected function runQuery(string $sql, array $params = []) {
$stmt = $this->db->prepare($sql);
$this->logQuery($sql, $params);
try {
$stmt->execute($params);
} catch (PDOException $e) {
if ($this->debug) {
error_log("SQL Hatası: " . $e->getMessage() . " - Sorgu: $sql");
}
throw $e;
}
return $stmt;
}
public function getQueryLog() {
return $this->queryLog;
}
public function getLastQuery() {
return end($this->queryLog);
}
public function select($sutunlar) {
$this->select = $sutunlar;
return $this;
}
public function join($tip, $tablo, $birinciKolon, $operator, $ikinciKolon) {
$this->joins[] = strtoupper($tip) . " JOIN $tablo ON $birinciKolon $operator $ikinciKolon";
return $this;
}
public function leftJoin($tablo, $birinciKolon, $operator, $ikinciKolon) {
return $this->join('LEFT', $tablo, $birinciKolon, $operator, $ikinciKolon);
}
public function innerJoin($tablo, $birinciKolon, $operator, $ikinciKolon) {
return $this->join('INNER', $tablo, $birinciKolon, $operator, $ikinciKolon);
}
public function rightJoin($tablo, $birinciKolon, $operator, $ikinciKolon) {
return $this->join('RIGHT', $tablo, $birinciKolon, $operator, $ikinciKolon);
}
public function where($kolon, $islem, $deger) {
$paramKey = ':w_' . count($this->params);
$this->wheres[] = "$kolon $islem $paramKey";
$this->params[$paramKey] = $deger;
return $this;
}
public function whereIn($kolon, array $degerler) {
if (empty($degerler)) {
// Boş array sorgu anlamı taşımaz
$this->wheres[] = "0=1";
return $this;
}
$placeholders = [];
foreach ($degerler as $i => $val) {
$key = ":w_in_" . count($this->params) + $i;
$placeholders[] = $key;
$this->params[$key] = $val;
}
$this->wheres[] = "$kolon IN (" . implode(',', $placeholders) . ")";
return $this;
}
public function orWhere($kolon, $islem, $deger) {
$paramKey = ':w_' . count($this->params);
if (empty($this->wheres)) {
$this->wheres[] = "$kolon $islem $paramKey";
} else {
$this->wheres[] = "OR $kolon $islem $paramKey";
}
$this->params[$paramKey] = $deger;
return $this;
}
public function whereNull($kolon) {
$this->wheres[] = "$kolon IS NULL";
return $this;
}
public function whereNotNull($kolon) {
$this->wheres[] = "$kolon IS NOT NULL";
return $this;
}
public function groupBy($sutun) {
$this->groupBy = " GROUP BY $sutun";
return $this;
}
public function having($kosul) {
$this->having = " HAVING $kosul";
return $this;
}
public function distinct($durum = true) {
$this->distinct = $durum;
return $this;
}
public function orderBy($kolon, $yon = 'ASC') {
$this->order = " ORDER BY $kolon $yon";
return $this;
}
public function limit($adet, $baslangic = null) {
$this->limit = " LIMIT " . ($baslangic !== null ? "$baslangic, " : "") . "$adet";
return $this;
}
protected function loadRelation(array &$models, string $relation) {
if (empty($models)) return;
$relatedModels = [];
$relatedModelClass = null;
// İlk nesneden ilişkili modeli al
$firstModel = $models[0];
if (!method_exists($firstModel, $relation)) {
throw new Exception("Relation method '$relation' not found on " . get_class($firstModel));
}
// İlk model üzerinden ilişkili model classını alıyoruz
$relatedSample = $firstModel->$relation();
if (is_array($relatedSample)) {
// hasMany
$relatedModelClass = get_class($relatedSample[0] ?? null);
} else {
// belongsTo veya single model
$relatedModelClass = get_class($relatedSample);
}
if (!$relatedModelClass) return;
// Ana modelin localKey alanlarını topla
$localKeys = array_map(fn($m) => $m->{$this->getLocalKey($relation)} ?? null, $models);
$localKeys = array_filter($localKeys);
if (empty($localKeys)) return;
// İlişkili modelde foreignKey ile toplu sorgu yap
$relatedModel = new $relatedModelClass();
// İlişki detaylarını model metodundan alalım:
// Burada convention (varsayılan) kullanalım. Dilersen genişletiriz.
$foreignKey = $this->getForeignKey($relation);
$relatedItems = $relatedModel->whereIn($foreignKey, $localKeys)->get();
// İlişki tipine göre eşle
if (is_array($relatedSample)) {
// hasMany: her ana modele ilişkili birden çok nesne dizisi bağla
foreach ($models as $model) {
$model->{$relation} = array_filter($relatedItems, fn($item) => $item->{$foreignKey} == $model->{$this->getLocalKey($relation)});
}
} else {
// belongsTo: tek nesne bağla
foreach ($models as $model) {
$model->{$relation} = current(array_filter($relatedItems, fn($item) => $item->{$this->getLocalKey($relation)} == $item->{$foreignKey})) ?: null;
}
}
}
public function get() {
$select = $this->distinct ? "DISTINCT {$this->select}" : $this->select;
$sql = "SELECT {$select} FROM {$this->tablo}";
if (!empty($this->joins)) {
$sql .= ' ' . implode(' ', $this->joins);
}
if (!empty($this->wheres)) {
$sql .= ' WHERE ' . implode(' ', $this->wheres);
}
$sql .= $this->groupBy;
$sql .= $this->having;
$sql .= $this->order;
$sql .= $this->limit;
$stmt = $this->runQuery($sql, $this->params);
$sonuclar = $stmt->fetchAll(PDO::FETCH_CLASS, get_class($this)); // model nesnesi olarak alıyoruz
// Eğer with() çağrıldıysa ilişkili modelleri yükle
if (!empty($this->with)) {
foreach ($this->with as $relation) {
$this->loadRelation($sonuclar, $relation);
}
}
$this->resetQuery();
return $sonuclar;
}
public function first() {
$this->limit(1);
$sonuc = $this->get();
return $sonuc[0] ?? null;
}
public function resetQuery() {
$this->wheres = [];
$this->params = [];
$this->order = '';
$this->limit = '';
$this->offset = '';
$this->joins = [];
$this->select = '*';
$this->with = [];
$this->groupBy = '';
$this->having = '';
$this->distinct = false;
}
public function find($id) {
$sql = "SELECT * FROM {$this->tablo} WHERE id = :id LIMIT 1";
$stmt = $this->runQuery($sql, ['id' => $id]);
return $stmt->fetch(PDO::FETCH_OBJ);
}
public function all() {
$sql = "SELECT * FROM {$this->tablo}";
return $this->db->query($sql)->fetchAll(PDO::FETCH_OBJ);
}
public function count() {
$sql = "SELECT COUNT(*) as toplam FROM {$this->tablo}";
if (!empty($this->wheres)) {
$sql .= ' WHERE ' . implode(' ', $this->wheres);
}
$stmt = $this->runQuery($sql, $this->params);
$this->resetQuery();
return (int) $stmt->fetchColumn();
}
public function exists() {
$sql = "SELECT 1 FROM {$this->tablo}";
if (!empty($this->wheres)) {
$sql .= ' WHERE ' . implode(' ', $this->wheres);
}
$sql .= " LIMIT 1";
$stmt = $this->runQuery($sql, $this->params);
$this->resetQuery();
return (bool) $stmt->fetchColumn();
}
public function pluck($sutun) {
$sql = "SELECT {$sutun} FROM {$this->tablo}";
if (!empty($this->wheres)) {
$sql .= ' WHERE ' . implode(' ', $this->wheres);
}
$sql .= $this->order;
$sql .= $this->limit;
$stmt = $this->runQuery($sql, $this->params);
$this->resetQuery();
return $stmt->fetchAll(PDO::FETCH_COLUMN);
}
public function firstPluck($sutun) {
$this->limit(1);
$sonuc = $this->pluck($sutun);
return $sonuc[0] ?? null;
}
public function hasMany($modelClass, $foreignKey, $localKey = 'id') {
$localValue = $this->{$localKey} ?? null;
if ($localValue === null) {
return [];
}
$model = new $modelClass();
return $model->where($foreignKey, '=', $localValue)->get();
}
public function belongsTo($modelClass, $foreignKey, $ownerKey = 'id') {
$foreignValue = $this->{$foreignKey} ?? null;
if ($foreignValue === null) {
return null;
}
$model = new $modelClass();
return $model->where($ownerKey, '=', $foreignValue)->first();
}
public function with(...$relations) {
$this->with = $relations;
return $this;
}
public function paginate($sayfa = 1, $adet = 10) {
$sayfa = max(1, (int)$sayfa);
$adet = max(1, (int)$adet);
$offset = ($sayfa - 1) * $adet;
// Toplam kayıt sayısını hesapla
$sayac = clone $this;
$toplam = $sayac->count();
// Sayfalı veriyi çek
$this->limit($adet, $offset);
$veriler = $this->get();
return [
'data' => $veriler,
'toplam' => $toplam,
'sayfa' => $sayfa,
'sayfa_sayisi' => ceil($toplam / $adet),
'adet' => $adet
];
}
protected function isFillable($alan) {
if (!empty($this->guarded) && in_array($alan, $this->guarded)) {
return false;
}
if (!empty($this->fillable)) {
return in_array($alan, $this->fillable);
}
return true; // ikisi de boşsa her şey set edilebilir
}
public function fill(array $veriler) {
foreach ($veriler as $alan => $deger) {
if ($this->isFillable($alan)) {
$this->$alan = $deger;
}
}
return $this;
}
protected function triggerEvent(string $event) {
if (method_exists($this, $event)) {
$this->$event();
}
}
public function save() {
$this->triggerEvent('beforeSave');
$veriler = get_object_vars($this);
// iç özellikleri temizle
unset($veriler['db'], $veriler['tablo'], $veriler['joins'], $veriler['wheres'],
$veriler['params'], $veriler['order'], $veriler['limit'], $veriler['offset'],
$veriler['with'], $veriler['select'], $veriler['groupBy'], $veriler['having'],
$veriler['distinct'], $veriler['primaryKey'], $veriler['timestamps'],
$veriler['createdAtColumn'], $veriler['updatedAtColumn']);
$id = $veriler[$this->primaryKey] ?? null;
$simdi = date('Y-m-d H:i:s');
if ($id) {
// Güncelleme
if ($this->timestamps && property_exists($this, $this->updatedAtColumn)) {
$this->{$this->updatedAtColumn} = $simdi;
$veriler[$this->updatedAtColumn] = $simdi;
}
$verilerKopya = $veriler;
unset($verilerKopya[$this->primaryKey]);
$set = [];
foreach ($verilerKopya as $key => $val) {
$set[] = "$key = :$key";
}
$sql = "UPDATE {$this->tablo} SET " . implode(', ', $set) . " WHERE {$this->primaryKey} = :{$this->primaryKey}";
$stmt = $this->runQuery($sql, $veriler);
$this->triggerEvent('afterSave');
return $stmt;
} else {
// Yeni kayıt
if ($this->timestamps) {
$this->{$this->createdAtColumn} = $simdi;
$this->{$this->updatedAtColumn} = $simdi;
$veriler[$this->createdAtColumn] = $simdi;
$veriler[$this->updatedAtColumn] = $simdi;
}
$alanlar = implode(', ', array_keys($veriler));
$degerler = ':' . implode(', :', array_keys($veriler));
$sql = "INSERT INTO {$this->tablo} ($alanlar) VALUES ($degerler)";
$basarili = $this->runQuery($sql, $veriler);
if ($basarili) {
$this->{$this->primaryKey} = $this->db->lastInsertId();
}
$this->triggerEvent('afterSave');
return $basarili;
}
}
public function create(array $veriler) {
$alanlar = implode(', ', array_keys($veriler));
$degerler = ':' . implode(', :', array_keys($veriler));
$sql = "INSERT INTO {$this->tablo} ($alanlar) VALUES ($degerler)";
return $this->runQuery($sql, $veriler);
}
public function update($id, array $veriler) {
$set = [];
foreach ($veriler as $key => $val) {
$set[] = "$key = :$key";
}
$sql = "UPDATE {$this->tablo} SET " . implode(', ', $set) . " WHERE id = :id";
$veriler['id'] = $id;
return $this->runQuery($sql, $veriler);
}
public function delete() {
if ($this->softDelete) {
return $this->softDelete();
}
return $this->forceDelete();
}
public function softDelete() {
if (!$this->softDelete) {
throw new Exception("Soft delete özelliği bu modelde aktif değil.");
}
$this->triggerEvent('beforeDelete');
$kolon = $this->deletedAtColumn;
$zaman = date('Y-m-d H:i:s');
$sql = "UPDATE {$this->tablo} SET $kolon = :zaman WHERE {$this->primaryKey} = :id";
$sonuc = $this->runQuery($sql, [
':zaman' => $zaman,
':id' => $this->{$this->primaryKey}
]);
$this->triggerEvent('afterDelete');
return $sonuc;
}
public function forceDelete() {
$this->triggerEvent('beforeDelete');
$sql = "DELETE FROM {$this->tablo} WHERE {$this->primaryKey} = :id";
$sonuc = $this->runQuery($sql, [
':id' => $this->{$this->primaryKey}
]);
$this->triggerEvent('afterDelete');
return $sonuc;
}
public function restore() {
if (!$this->softDelete) {
throw new Exception("Restore özelliği bu modelde aktif değil.");
}
$kolon = $this->deletedAtColumn;
$sql = "UPDATE {$this->tablo} SET $kolon = NULL WHERE {$this->primaryKey} = :id";
return $this->runQuery($sql, [
':id' => $this->{$this->primaryKey}
]);
}
public function onlyTrashed() {
return $this->whereNotNull($this->deletedAtColumn);
}
public function withTrashed() {
// get()'e otomatik WHERE koymak yerine tamamen serbest bırakıyoruz
return $this; // future flag eklenebilir
}
public function toArray() {
$veriler = get_object_vars($this);
// Dahili alanları ayıklıyoruz
unset($veriler['db'], $veriler['tablo'], $veriler['joins'], $veriler['wheres'],
$veriler['params'], $veriler['order'], $veriler['limit'], $veriler['offset'],
$veriler['with'], $veriler['select'], $veriler['groupBy'], $veriler['having'],
$veriler['distinct'], $veriler['primaryKey'], $veriler['softDelete'],
$veriler['timestamps'], $veriler['createdAtColumn'], $veriler['updatedAtColumn'],
$veriler['deletedAtColumn']);
// Eager-loaded ilişkileri kontrol et
foreach ($this->with as $relation) {
if (isset($this->$relation)) {
$ilişkili = $this->$relation;
if (is_array($ilişkili)) {
$veriler[$relation] = array_map(fn($item) => $item instanceof self ? $item->toArray() : $item, $ilişkili);
} elseif ($ilişkili instanceof self) {
$veriler[$relation] = $ilişkili->toArray();
} else {
$veriler[$relation] = $ilişkili;
}
}
}
return $veriler;
}
public static function collectionToArray(array $liste) {
return array_map(fn($item) => $item->toArray(), $liste);
}
public function toJson($options = 0) {
return json_encode($this->toArray(), $options);
}
public static function collectionToJson(array $liste, $options = 0) {
return json_encode(self::collectionToArray($liste), $options);
}
protected function validateRequired($alan, $deger, $params) {
if ($deger === null || $deger === '') {
$this->errors[$alan][] = "$alan alanı zorunludur.";
}
}
protected function validateEmail($alan, $deger, $params) {
if ($deger && !filter_var($deger, FILTER_VALIDATE_EMAIL)) {
$this->errors[$alan][] = "$alan geçerli bir e-posta olmalıdır.";
}
}
protected function validateMin($alan, $deger, $params) {
if (is_numeric($deger) && $deger < (int)$params) {
$this->errors[$alan][] = "$alan alanı minimum $params değerinde olmalıdır.";
} elseif (is_string($deger) && mb_strlen($deger) < (int)$params) {
$this->errors[$alan][] = "$alan alanı minimum $params karakter olmalıdır.";
}
}
protected function validateInteger($alan, $deger, $params) {
if ($deger !== null && !filter_var($deger, FILTER_VALIDATE_INT)) {
$this->errors[$alan][] = "$alan alanı tamsayı olmalıdır.";
}
}
protected function validateRegex($alan, $deger, $params) {
if ($deger && !preg_match($params, $deger)) {
$this->errors[$alan][] = "$alan alanı geçerli formatta değil.";
}
}
protected function validateUnique($alan, $deger, $params) {
$sql = "SELECT COUNT(*) FROM {$this->tablo} WHERE $alan = :deger";
// Güncelleme sırasında kendi kaydını saymamak için id kontrolü
if (isset($this->{$this->primaryKey})) {
$sql .= " AND {$this->primaryKey} != :id";
}
$paramsArr = [':deger' => $deger];
if (isset($this->{$this->primaryKey})) {
$paramsArr[':id'] = $this->{$this->primaryKey};
}
$stmt = $this->runQuery($sql, $paramsArr);
if ($stmt->fetchColumn() > 0) {
$this->errors[$alan][] = "$alan zaten kullanılıyor.";
}
}
protected function validateCallback($alan, $deger, $params) {
if (method_exists($this, $params)) {
$this->$params($alan, $deger);
} else {
$this->errors[$alan][] = "Geçersiz callback fonksiyon: $params";
}
}
public function validate() {
$this->errors = [];
foreach ($this->rules as $alan => $kurallar) {
$deger = $this->$alan ?? null;
foreach ($kurallar as $kural) {
$params = null;
if (strpos($kural, ':') !== false) {
[$kural, $params] = explode(':', $kural, 2);
}
$method = 'validate' . ucfirst($kural);
if (method_exists($this, $method)) {
$this->$method($alan, $deger, $params);
}
}
}
return empty($this->errors);
}
public function getErrors() {
return $this->errors;
}
}