4
4
* please don't remove this comment block
5
5
*
6
6
* @author phptricks Team - Mohammad Anzawi
7
- * @author_uri http ://phptricks.org
7
+ * @author_uri https ://phptricks.org
8
8
* @uri https://github.com/anzawi/php-database-class
9
- * @version 3.0 .0
9
+ * @version 3.1 .0
10
10
* @licence MIT -> https://opensource.org/licenses/MIT
11
11
* @package PHPtricks\Database
12
12
*/
@@ -30,10 +30,6 @@ class Database implements \IteratorAggregate, \ArrayAccess
30
30
private static $ _instance ;
31
31
32
32
private
33
- /**
34
- * @var $_pdo object PDO object
35
- */
36
- $ _pdo ,
37
33
/**
38
34
* @var $_query string store sql statement
39
35
*/
@@ -70,6 +66,10 @@ class Database implements \IteratorAggregate, \ArrayAccess
70
66
71
67
72
68
protected
69
+ /**
70
+ * @var $_pdo object PDO object
71
+ */
72
+ $ _pdo ,
73
73
/**
74
74
* @var $_table string current table name
75
75
*/
@@ -105,13 +105,23 @@ protected function __construct($data = [], $info = [])
105
105
}
106
106
}
107
107
108
+ // public function __call($method, $args)
109
+ // {
110
+ // var_dump($this);
111
+ // }
112
+
108
113
/**
109
114
* @param $prop
110
115
* @return mixed
111
116
*/
112
117
public function __get ($ prop )
113
118
{
114
- return isset ($ this ->_results ->$ prop ) ? $ this ->_results ->$ prop : null ;
119
+ if (!isset ($ this ->__cach [md5 ($ this ->_table )]))
120
+ {
121
+ return null ;
122
+ }
123
+
124
+ return isset ($ this ->_results ->$ prop ) ? $ this ->_results ->$ prop : null ;
115
125
}
116
126
117
127
public function __set ($ prop , $ value )
@@ -245,7 +255,7 @@ public static function connect()
245
255
// check if $_instance is null or not
246
256
// if null so connect database
247
257
// otherwise return current connection object
248
- if (!isset (self ::$ _instance )) {
258
+ if (!isset (self ::$ _instance ) || self :: $ _instance == null ) {
249
259
self ::$ _instance = new Database ();
250
260
}
251
261
@@ -399,16 +409,21 @@ public function save()
399
409
{
400
410
$ x = 1 ;
401
411
$ this ->_query = "WHERE " ;
402
- foreach ($ this ->results () as $ i => $ row )
403
- {
404
- $ this ->_query .= " {$ i } = {$ row }" ;
412
+
413
+ foreach ($ this ->results () as $ i => $ row )
414
+ {
415
+ if (!is_numeric ($ row ))
416
+ $ this ->_query .= " {$ i } = ' {$ row }' " ;
417
+ else
418
+ $ this ->_query .= " {$ i } = {$ row }" ;
405
419
// add comma between values
406
420
if ($ x < count ((array )$ this ->results ())) {
407
421
$ this ->_query .= " AND " ;
408
422
}
409
423
410
424
$ x ++;
411
425
}
426
+
412
427
return $ this ->update ((array )$ this ->getNewValues ());
413
428
}
414
429
@@ -431,7 +446,29 @@ public function select($fields = ['*'])
431
446
. " FROM {$ this ->_table } {$ this ->_query }" ;
432
447
433
448
$ this ->_query = $ sql ;
434
- return new Database ($ this ->query ($ sql )->results (), ['table ' => $ this ->_table , 'id ' => $ this ->_idColumn ]);
449
+
450
+ return $ this ->collection ([
451
+ 'results ' => $ this ->query ($ sql )->results (),
452
+ 'table ' => $ this ->_table ,
453
+ 'id ' => $ this ->_idColumn
454
+ ]);
455
+
456
+ // return new Database($this->query($sql)->results(), ['table' => $this->_table, 'id' => $this->_idColumn]);
457
+ }
458
+
459
+ protected function collection ($ collection )
460
+ {
461
+ return new Collection ($ collection , self ::$ _instance );
462
+ }
463
+
464
+ protected function getCollection ($ table )
465
+ {
466
+ if (isset ($ this ->__cach [md5 ($ table )]))
467
+ {
468
+ return true ;
469
+ }
470
+
471
+ return false ;
435
472
}
436
473
437
474
/**
@@ -440,14 +477,15 @@ public function select($fields = ['*'])
440
477
*/
441
478
public function delete ()
442
479
{
480
+ $ results = (array )$ this ->_results ;
443
481
if ($ this ->count () == 1 )
444
482
{
445
- return $ this ->remove ($ this -> _results );
483
+ return $ this ->remove ($ results );
446
484
}
447
485
448
486
for ($ i = 0 ; $ this ->count () > $ i ; $ i ++)
449
487
{
450
- $ this ->remove ($ this -> _results [$ i ]);
488
+ $ this ->remove ( $ results [$ i ]);
451
489
}
452
490
453
491
return true ;
@@ -457,9 +495,13 @@ private function remove($data)
457
495
{
458
496
$ this ->_where = "WHERE " ;
459
497
$ x = 1 ;
498
+
460
499
foreach ($ data as $ i => $ row )
461
500
{
462
- $ this ->_where .= " {$ i } = {$ row }" ;
501
+ if (!is_numeric ($ row ))
502
+ $ this ->_where .= " {$ i } = ' {$ row }' " ;
503
+ else
504
+ $ this ->_where .= " {$ i } = {$ row }" ;
463
505
// add comma between values
464
506
if ($ x < count ((array )$ data )) {
465
507
$ this ->_where .= " AND " ;
@@ -477,16 +519,8 @@ private function remove($data)
477
519
*/
478
520
public function find ($ id )
479
521
{
480
- $ result = $ this ->where ($ this ->_idColumn , $ id )
481
- ->first ()->results ();
482
-
483
-
484
- if (count ((array )$ result ))
485
- {
486
- return new Database ($ result , ['id ' => $ this ->_idColumn , 'table ' => $ this ->_table ]);
487
- }
488
-
489
- return new Database ([], ['id ' => $ this ->_idColumn , 'table ' => $ this ->_table ]);
522
+ return $ result = $ this ->where ($ this ->_idColumn , $ id )
523
+ ->first ();
490
524
}
491
525
492
526
/**
@@ -622,9 +656,18 @@ public function first()
622
656
623
657
if (count ((array )$ results ))
624
658
{
625
- return new Database ($ results [0 ], ['table ' => $ this ->_table , 'id ' => $ this ->_idColumn ]);
659
+ return $ this ->collection ([
660
+ 'results ' => $ results [0 ],
661
+ 'table ' => $ this ->_table ,
662
+ 'id ' => $ this ->_idColumn
663
+ ]);
626
664
}
627
- return new Database ([], ['table ' => $ this ->_table , 'id ' => $ this ->_idColumn ]);
665
+
666
+ return $ this ->collection ([
667
+ 'results ' => [],
668
+ 'table ' => $ this ->_table ,
669
+ 'id ' => $ this ->_idColumn
670
+ ]);
628
671
}
629
672
630
673
public function firstRecord ()
@@ -1284,4 +1327,105 @@ private function getNewValues()
1284
1327
{
1285
1328
return $ this ->_newValues ;
1286
1329
}
1330
+ }
1331
+
1332
+ class Collection extends Database
1333
+ {
1334
+ public function __construct ($ data , $ connection = null )
1335
+ {
1336
+ if (isset ($ connection ))
1337
+ {
1338
+ $ this ->_table = $ data ['table ' ];
1339
+ $ this ->_results = $ data ['results ' ];
1340
+ $ this ->_idColumn = $ data ['id ' ];
1341
+ $ this ->_pdo = $ connection ->_pdo ;
1342
+ }
1343
+ else
1344
+ $ this ->_results = $ data ;
1345
+ }
1346
+
1347
+ public function all ()
1348
+ {
1349
+ return $ this ->results ();
1350
+ }
1351
+
1352
+ public function empty ()
1353
+ {
1354
+ return empty ($ this ->_results );
1355
+ }
1356
+
1357
+ public function first ()
1358
+ {
1359
+ return isset ($ this ->_results [0 ]) ? $ this ->_results [0 ] : null ;
1360
+ }
1361
+
1362
+ public function last ()
1363
+ {
1364
+ $ reverse = array_reverse ($ this ->results ());
1365
+
1366
+ return isset ($ reverse [0 ]) ? $ reverse [0 ] : null ;
1367
+ }
1368
+
1369
+ public function each (callable $ callback )
1370
+ {
1371
+ foreach ($ this ->results () as $ key => $ value )
1372
+ {
1373
+ $ callback ($ value , $ key );
1374
+ }
1375
+
1376
+ return $ this ;
1377
+ }
1378
+
1379
+ public function filter (callable $ callback = null )
1380
+ {
1381
+ if ($ callback )
1382
+ {
1383
+ return new static (array_filter ($ this ->results (), $ callback ));
1384
+ }
1385
+
1386
+ // exclude null and empty
1387
+ return new static (array_filter ($ this ->results ()));
1388
+ }
1389
+
1390
+ public function keys ()
1391
+ {
1392
+ return new static (array_keys ($ this ->results ()));
1393
+ }
1394
+
1395
+ public function map (callable $ callback )
1396
+ {
1397
+ $ keys = $ this ->keys ()->all ();
1398
+ $ results = array_map ($ callback , $ this ->results (), $ keys );
1399
+
1400
+ return new static (array_combine ($ keys , $ results ));
1401
+ }
1402
+
1403
+
1404
+ public function toJson ()
1405
+ {
1406
+ return json_encode ($ this ->results ());
1407
+ }
1408
+
1409
+ public function __toString ()
1410
+ {
1411
+ return $ this ->toJson ();
1412
+ }
1413
+
1414
+ public function merge ($ items )
1415
+ {
1416
+ return new static (
1417
+ array_merge (
1418
+ $ this ->results (),
1419
+ $ this ->toArray ($ items )
1420
+ )
1421
+ );
1422
+ }
1423
+
1424
+ protected function toArray ($ items )
1425
+ {
1426
+ if (!is_array ($ items ) && $ items instanceof Collection)
1427
+ return $ items ->all ();
1428
+
1429
+ return $ items ;
1430
+ }
1287
1431
}
0 commit comments