We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7923
    • 4,213 Posts
    I have a code like this:

    $retArray = array();
    $polylineMarkerObj = $modx->newObject('ltPolylineMarker');
    $graph = '{
    	"PrincipalRoute":{
    		"Polyline":{
    			"PolylineMarker":{
    			}
    		}
    	},
    	"SchoolMarker":{
    	}
    }';
    $criteria = $modx->newQuery('ltPrincipalMap');
    $criteria->where(array(
    	'ltPrincipalMap.user_id' => $modx->user->get('id'),
    	'PolylineMarker.type' => $polylineMarkerObj::TYPE_PATH_MARKER
    ));
    $criteria->sortby('PrincipalRoute.index','ASC');
    $criteria->bindGraph($graph);
    //$criteria->prepare();
    //$modx->log(modX::LOG_LEVEL_ERROR, $criteria->toSQL());
    $principalMap = $modx->getObjectGraph('ltPrincipalMap', $graph, $criteria);
    if (is_object($principalMap)) {
    	$schoolMarker = $principalMap->getOne('SchoolMarker');
    	$retArray['schoolMarker']['lat'] = $schoolMarker->get('lat');
    	$retArray['schoolMarker']['lng'] = $schoolMarker->get('lng');
    	$retArray['polylines'] = array();
    	$principalRoutes = $principalMap->getMany('PrincipalRoute');
    	$index = 0;
    	foreach ($principalRoutes as $principalRoute) {
    		$retArray['polylines'][$index] = array();
    		$polyline = $principalRoute->getOne('Polyline');
    		$polylineMarkers = $polyline->getMany('PolylineMarker', array('type' => $polylineMarkerObj::TYPE_PATH_MARKER));
    		$idx = 0;
    		foreach ($polylineMarkers as $polylineMarker) {
    			$retArray['polylines'][$index]['path'][$idx] = array('lat' => $polylineMarker->get('lat'), 'lng' => $polylineMarker->get('lng'));
    			$idx++;
    		}
    		$index++;
    	}
    	return '{success:true, data:'.json_encode($retArray).'}';
    } else {
    	return '{success:false, msg:"could retrieve principal map"}';
    }
    


    How can I check that xpdo doesn't create database request each time I call the objects that are in the graph with getOne and getMany?

    I'm asking this because I'm setting a criteria to the getObjectGraph() on line 16 above that it should retrieve markers only with certain type. But when I first had the getMany() call on line 33 without the type condition, it returned all related markers. I should have thought that it's not possible to get them from the graph because I had limited it to only certain markers in the getObjectGraph() call. So it seems that atleast when I don't have the type confition on line 33, it goes to the database at that point to retrieve all related objects.

    So.. How can I check that does the code above visit the database more than once?


      "He can have a lollipop any time he wants to. That's what it means to be a programmer."
      • 3749
      • 24,544 Posts
      You shouldn't need the getOne() and getMany() calls, if the graph is correct, everything you need should be there in the object returned from getObjectGraph(). E.g.:


      $retArray['schoolMarker']['lat'] = $principalMap->SchoolMarker->get('lat');
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 7923
        • 4,213 Posts
        Oh.. ofcourse! Thanks!

        I changed it now to this below. Do you have any guess why I have to have on line 33 the getMany() call for it to get only the "PolylineMarker" with type set to certain value. I have set the same criteria to the graph, but if I use $polyline->PolylineMarker there, it returns all related markers and not only those that have certain type.

        $retArray = array();
        $polylineMarkerObj = $modx->newObject('ltPolylineMarker');
        $graph = '{
        	"PrincipalRoute":{
        		"Polyline":{
        			"PolylineMarker":{
        			}
        		}
        	},
        	"SchoolMarker":{
        	}
        }';
        $criteria = $modx->newQuery('ltPrincipalMap');
        $criteria->where(array(
        	'ltPrincipalMap.user_id' => $modx->user->get('id'),
        	'PolylineMarker.type' => $polylineMarkerObj::TYPE_PATH_MARKER
        ));
        $criteria->sortby('PrincipalRoute.index','ASC');
        $criteria->bindGraph($graph);
        //$criteria->prepare();
        //$modx->log(modX::LOG_LEVEL_ERROR, $criteria->toSQL());
        $principalMap = $modx->getObjectGraph('ltPrincipalMap', $graph, $criteria);
        if (is_object($principalMap)) {
        	$schoolMarker = $principalMap->SchoolMarker;
        	$retArray['schoolMarker']['lat'] = $schoolMarker->get('lat');
        	$retArray['schoolMarker']['lng'] = $schoolMarker->get('lng');
        	$retArray['polylines'] = array();
        	$principalRoutes = $principalMap->PrincipalRoute;
        	$index = 0;
        	foreach ($principalRoutes as $principalRoute) {
        		$retArray['polylines'][$index] = array();
        		$polyline = $principalRoute->Polyline;
        		$polylineMarkers = $polyline->getMany('PolylineMarker', array('type' => $polylineMarkerObj::TYPE_PATH_MARKER));
        		$idx = 0;
        		foreach ($polylineMarkers as $polylineMarker) {
        			$retArray['polylines'][$index]['path'][$idx] = array('lat' => $polylineMarker->get('lat'), 'lng' => $polylineMarker->get('lng'));
        			$idx++;
        		}
        		$index++;
        	}
        	return '{success:true, data:'.json_encode($retArray).'}';
        } else {
        	return '{success:false, msg:"could retrieve principal map"}';
        }
        


          "He can have a lollipop any time he wants to. That's what it means to be a programmer."
          • 3749
          • 24,544 Posts
          It looks like your where() clause should be filtering those. I don't know why it's not working.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 7923
            • 4,213 Posts
            I don't get it either, because if I log the graph sql on the line 21 there, it returns this:

            SELECT 
              `ltPrincipalMap`.`id` AS `ltPrincipalMap_id`, 
              `ltPrincipalMap`.`user_id` AS `ltPrincipalMap_user_id`, 
              `ltPrincipalMap`.`school_marker_id` AS `ltPrincipalMap_school_marker_id`, 
              `ltPrincipalMap`.`submitted` AS `ltPrincipalMap_submitted`, 
              `ltPrincipalMap`.`created_on` AS `ltPrincipalMap_created_on`, 
              `ltPrincipalMap`.`created_user_id` AS `ltPrincipalMap_created_user_id`, 
              `ltPrincipalMap`.`created_user_ip` AS `ltPrincipalMap_created_user_ip`, 
              `ltPrincipalMap`.`updated_on` AS `ltPrincipalMap_updated_on`, 
              `ltPrincipalMap`.`updated_user_id` AS `ltPrincipalMap_updated_user_id`, 
              `ltPrincipalMap`.`updated_user_ip` AS `ltPrincipalMap_updated_user_ip`, 
              `PrincipalRoute`.`id` AS `PrincipalRoute_id`, 
              `PrincipalRoute`.`polyline_id` AS `PrincipalRoute_polyline_id`, 
              `PrincipalRoute`.`principal_map_id` AS `PrincipalRoute_principal_map_id`, 
              `PrincipalRoute`.`index` AS `PrincipalRoute_index`, 
              `PrincipalRoute`.`created_on` AS `PrincipalRoute_created_on`, 
              `PrincipalRoute`.`created_user_id` AS `PrincipalRoute_created_user_id`, 
              `PrincipalRoute`.`created_user_ip` AS `PrincipalRoute_created_user_ip`, 
              `PrincipalRoute`.`updated_on` AS `PrincipalRoute_updated_on`, 
              `PrincipalRoute`.`updated_user_id` AS `PrincipalRoute_updated_user_id`, 
              `PrincipalRoute`.`updated_user_ip` AS `PrincipalRoute_updated_user_ip`, 
              `Polyline`.`id` AS `Polyline_id`, 
              `Polyline`.`created_on` AS `Polyline_created_on`, 
              `Polyline`.`created_user_id` AS `Polyline_created_user_id`, 
              `Polyline`.`created_user_ip` AS `Polyline_created_user_ip`, 
              `Polyline`.`updated_on` AS `Polyline_updated_on`, 
              `Polyline`.`updated_user_id` AS `Polyline_updated_user_id`, 
              `Polyline`.`updated_user_ip` AS `Polyline_updated_user_ip`, 
              `PolylineMarker`.`id` AS `PolylineMarker_id`, 
              `PolylineMarker`.`polyline_id` AS `PolylineMarker_polyline_id`, 
              `PolylineMarker`.`type` AS `PolylineMarker_type`, 
              `PolylineMarker`.`lat` AS `PolylineMarker_lat`, 
              `PolylineMarker`.`lng` AS `PolylineMarker_lng`, 
              `PolylineMarker`.`created_on` AS `PolylineMarker_created_on`, 
              `PolylineMarker`.`created_user_id` AS `PolylineMarker_created_user_id`, 
              `PolylineMarker`.`created_user_ip` AS `PolylineMarker_created_user_ip`, 
              `PolylineMarker`.`updated_on` AS `PolylineMarker_updated_on`, 
              `PolylineMarker`.`updated_user_id` AS `PolylineMarker_updated_user_id`, 
              `PolylineMarker`.`updated_user_ip` AS `PolylineMarker_updated_user_ip`, 
              `SchoolMarker`.`id` AS `SchoolMarker_id`, 
              `SchoolMarker`.`type` AS `SchoolMarker_type`, 
              `SchoolMarker`.`lat` AS `SchoolMarker_lat`, 
              `SchoolMarker`.`lng` AS `SchoolMarker_lng`, 
              `SchoolMarker`.`created_on` AS `SchoolMarker_created_on`, 
              `SchoolMarker`.`created_user_id` AS `SchoolMarker_created_user_id`, 
              `SchoolMarker`.`created_user_ip` AS `SchoolMarker_created_user_ip`, 
              `SchoolMarker`.`updated_on` AS `SchoolMarker_updated_on`, 
              `SchoolMarker`.`updated_user_id` AS `SchoolMarker_updated_user_id`, 
              `SchoolMarker`.`updated_user_ip` AS `SchoolMarker_updated_user_ip`
            FROM 
              `custom_principal_map` AS `ltPrincipalMap` 
            LEFT JOIN 
              `custom_principal_route` `PrincipalRoute` ON `ltPrincipalMap`.`id` = `PrincipalRoute`.`principal_map_id` 
            LEFT JOIN 
              `custom_polyline` `Polyline` ON `PrincipalRoute`.`polyline_id` = `Polyline`.`id` 
            LEFT JOIN 
              `custom_polyline_marker` `PolylineMarker` ON `Polyline`.`id` = `PolylineMarker`.`polyline_id` 
            LEFT JOIN 
              `custom_marker` `SchoolMarker` ON `ltPrincipalMap`.`school_marker_id` = `SchoolMarker`.`id` 
            WHERE  
              ( `ltPrincipalMap`.`user_id` = 2 AND `PolylineMarker`.`type` = '3' )  
            ORDER BY 
              PrincipalRoute.index ASC, `ltPrincipalMap`.`id` ASC 
            


            And if I run that in phpMyAdmin, it returns the correct result set and "PolylineMarker" is filtered correctly. But still on line 33 if I use:

            $polylineMarkers = $polyline->PolylineMarker;
            


            It retrieves all of the related markers and not only those with type set to 3. What's going on... ...does jason still visit these forums? wink

            Do you Bob know that if I use this on the line 33:

            $polylineMarkers = $polyline->getMany('PolylineMarker', array('type' => $polylineMarkerObj::TYPE_PATH_MARKER));
            


            Does it go to the database to retrieve the objects? Is there a way for me to check this other than using mysqld logging?


              "He can have a lollipop any time he wants to. That's what it means to be a programmer."
              • 3749
              • 24,544 Posts
              I'm not sure about getMany() there. It may pull them from the cache. Jason would know. Hopefully he'll drop by.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting