@@ -131,6 +131,9 @@ public function __set ($name, $value) {
131131 * @return mixed
132132 */
133133 public function __get ($ name ) {
134+ if (isset ($ this ->data [$ name ]) && $ this ->data [$ name ] instanceof dbObject)
135+ return $ this ->data [$ name ];
136+
134137 if (property_exists ($ this , 'relations ' ) && isset ($ this ->relations [$ name ])) {
135138 $ relationType = strtolower ($ this ->relations [$ name ][0 ]);
136139 $ modelName = $ this ->relations [$ name ][1 ];
@@ -306,10 +309,11 @@ private function byId ($id, $fields = null) {
306309 * @return dbObject
307310 */
308311 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 );
310314 $ this ->processArrays ($ results );
311315 $ this ->data = $ results ;
312- $ this ->processWith ($ results );
316+ $ this ->processAllWith ($ results );
313317 if ($ this ->returnType == 'Json ' )
314318 return json_encode ($ results );
315319 if ($ this ->returnType == 'Array ' )
@@ -333,17 +337,19 @@ private function getOne ($fields = null) {
333337 */
334338 private function get ($ limit = null , $ fields = null ) {
335339 $ objects = Array ();
336- $ results = $ this ->db ->get ($ this ->dbTable , $ limit , $ fields );
340+ $ this ->processHasOneWith ();
341+ $ results = $ this ->db ->ArrayBuilder ()->get ($ this ->dbTable , $ limit , $ fields );
337342 foreach ($ results as &$ r ) {
338343 $ this ->processArrays ($ r );
339344 $ this ->data = $ r ;
340- $ this ->processWith ($ r );
345+ $ this ->processAllWith ($ r, false );
341346 if ($ this ->returnType == 'Object ' ) {
342347 $ item = new static ($ r );
343348 $ item ->isNew = false ;
344349 $ objects [] = $ item ;
345350 }
346351 }
352+ $ this ->_with = Array ();
347353 if ($ this ->returnType == 'Object ' )
348354 return $ objects ;
349355
@@ -362,7 +368,10 @@ private function get ($limit = null, $fields = null) {
362368 * @return dbObject
363369 */
364370 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 ];
366375
367376 return $ this ;
368377 }
@@ -393,7 +402,7 @@ private function join ($objectName, $key = null, $joinType = 'LEFT') {
393402 * @return int
394403 */
395404 private function count () {
396- $ res = $ this ->db ->getValue ($ this ->dbTable , "count(*) " );
405+ $ res = $ this ->db ->ArrayBuilder ()-> getValue ($ this ->dbTable , "count(*) " );
397406 return $ res ['cnt ' ];
398407 }
399408
@@ -457,7 +466,7 @@ public static function __callStatic ($method, $arg) {
457466 */
458467 public function toArray () {
459468 $ data = $ this ->data ;
460- $ this ->processWith ($ data );
469+ $ this ->processAllWith ($ data );
461470 foreach ($ data as &$ d ) {
462471 if ($ d instanceof dbObject)
463472 $ d = $ d ->data ;
@@ -484,15 +493,59 @@ public function __toString () {
484493 }
485494
486495 /**
496+ * Function queries hasMany relations if needed and also converts hasOne object names
497+ *
487498 * @param array $data
488499 */
489- private function processWith (&$ data ) {
500+ private function processAllWith (&$ data, $ shouldReset = true ) {
490501 if (count ($ this ->_with ) == 0 )
491502 return ;
492- foreach ($ this ->_with as $ w )
493- $ data [$ w ] = $ this ->$ w ;
494503
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+ }
496549 }
497550
498551 /**
0 commit comments