@@ -131,6 +131,9 @@ public function __set ($name, $value) {
131
131
* @return mixed
132
132
*/
133
133
public function __get ($ name ) {
134
+ if (isset ($ this ->data [$ name ]) && $ this ->data [$ name ] instanceof dbObject)
135
+ return $ this ->data [$ name ];
136
+
134
137
if (property_exists ($ this , 'relations ' ) && isset ($ this ->relations [$ name ])) {
135
138
$ relationType = strtolower ($ this ->relations [$ name ][0 ]);
136
139
$ modelName = $ this ->relations [$ name ][1 ];
@@ -306,10 +309,11 @@ private function byId ($id, $fields = null) {
306
309
* @return dbObject
307
310
*/
308
311
private function getOne ($ fields = null ) {
309
- $ results = $ this ->db ->getOne ($ this ->dbTable , $ fields );
312
+ $ this ->processHasOneWith ();
313
+ $ results = $ this ->db ->ArrayBuilder ()->getOne ($ this ->dbTable , $ fields );
310
314
$ this ->processArrays ($ results );
311
315
$ this ->data = $ results ;
312
- $ this ->processWith ($ results );
316
+ $ this ->processAllWith ($ results );
313
317
if ($ this ->returnType == 'Json ' )
314
318
return json_encode ($ results );
315
319
if ($ this ->returnType == 'Array ' )
@@ -333,17 +337,19 @@ private function getOne ($fields = null) {
333
337
*/
334
338
private function get ($ limit = null , $ fields = null ) {
335
339
$ objects = Array ();
336
- $ results = $ this ->db ->get ($ this ->dbTable , $ limit , $ fields );
340
+ $ this ->processHasOneWith ();
341
+ $ results = $ this ->db ->ArrayBuilder ()->get ($ this ->dbTable , $ limit , $ fields );
337
342
foreach ($ results as &$ r ) {
338
343
$ this ->processArrays ($ r );
339
344
$ this ->data = $ r ;
340
- $ this ->processWith ($ r );
345
+ $ this ->processAllWith ($ r, false );
341
346
if ($ this ->returnType == 'Object ' ) {
342
347
$ item = new static ($ r );
343
348
$ item ->isNew = false ;
344
349
$ objects [] = $ item ;
345
350
}
346
351
}
352
+ $ this ->_with = Array ();
347
353
if ($ this ->returnType == 'Object ' )
348
354
return $ objects ;
349
355
@@ -362,7 +368,10 @@ private function get ($limit = null, $fields = null) {
362
368
* @return dbObject
363
369
*/
364
370
private function with ($ objectName ) {
365
- $ this ->_with [] = $ objectName ;
371
+ if (!property_exists ($ this , 'relations ' ) && !isset ($ this ->relations [$ name ]))
372
+ die ("No relation with name $ objectName found " );
373
+
374
+ $ this ->_with [$ objectName ] = $ this ->relations [$ objectName ];
366
375
367
376
return $ this ;
368
377
}
@@ -393,7 +402,7 @@ private function join ($objectName, $key = null, $joinType = 'LEFT') {
393
402
* @return int
394
403
*/
395
404
private function count () {
396
- $ res = $ this ->db ->getValue ($ this ->dbTable , "count(*) " );
405
+ $ res = $ this ->db ->ArrayBuilder ()-> getValue ($ this ->dbTable , "count(*) " );
397
406
return $ res ['cnt ' ];
398
407
}
399
408
@@ -457,7 +466,7 @@ public static function __callStatic ($method, $arg) {
457
466
*/
458
467
public function toArray () {
459
468
$ data = $ this ->data ;
460
- $ this ->processWith ($ data );
469
+ $ this ->processAllWith ($ data );
461
470
foreach ($ data as &$ d ) {
462
471
if ($ d instanceof dbObject)
463
472
$ d = $ d ->data ;
@@ -484,15 +493,59 @@ public function __toString () {
484
493
}
485
494
486
495
/**
496
+ * Function queries hasMany relations if needed and also converts hasOne object names
497
+ *
487
498
* @param array $data
488
499
*/
489
- private function processWith (&$ data ) {
500
+ private function processAllWith (&$ data, $ shouldReset = true ) {
490
501
if (count ($ this ->_with ) == 0 )
491
502
return ;
492
- foreach ($ this ->_with as $ w )
493
- $ data [$ w ] = $ this ->$ w ;
494
503
495
- $ this ->_with = Array ();
504
+ foreach ($ this ->_with as $ name => $ opts ) {
505
+ $ relationType = strtolower ($ opts [0 ]);
506
+ $ modelName = $ opts [1 ];
507
+ if ($ relationType == 'hasone ' ) {
508
+ $ obj = new $ modelName ;
509
+ $ table = $ obj ->dbTable ;
510
+
511
+ if (!isset ($ data [$ table ])) {
512
+ $ data [$ name ] = $ this ->$ name ;
513
+ continue ;
514
+ }
515
+ if ($ this ->returnType == 'Object ' ) {
516
+ $ item = new $ modelName ($ data [$ table ]);
517
+ $ item ->returnType = $ this ->returnType ;
518
+ $ item ->isNew = false ;
519
+ $ data [$ name ] = $ item ;
520
+ } else {
521
+ $ data [$ name ] = $ data [$ table ];
522
+ }
523
+ unset ($ data [$ table ]);
524
+ }
525
+ else
526
+ $ data [$ name ] = $ this ->$ name ;
527
+ }
528
+ if ($ shouldReset )
529
+ $ this ->_with = Array ();
530
+ }
531
+
532
+ /*
533
+ * Function building hasOne joins for get/getOne method
534
+ */
535
+ private function processHasOneWith () {
536
+ if (count ($ this ->_with ) == 0 )
537
+ return ;
538
+ foreach ($ this ->_with as $ name => $ opts ) {
539
+ $ relationType = strtolower ($ opts [0 ]);
540
+ $ modelName = $ opts [1 ];
541
+ $ key = null ;
542
+ if (isset ($ opts [2 ]))
543
+ $ key = $ opts [2 ];
544
+ if ($ relationType == 'hasone ' ) {
545
+ $ this ->db ->setQueryOption ("MYSQLI_NESTJOIN " );
546
+ $ this ->join ($ modelName , $ key );
547
+ }
548
+ }
496
549
}
497
550
498
551
/**
0 commit comments