We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40092
    • 265 Posts
    Ah yes, i know, i tend to mix those things up ;-)

    Just to be clear, getCollection returns an array of objects and my previous mysql_fetch_object returns AN object, right?

    Turning $fields into an array won't do any good in this case, i guess?
    Is there a way in xPDO to return an object exactly as i previously did, or should i convert something ?

      • 40092
      • 265 Posts
      Example code of what i used to use, now want to "translate" to xPDO (there's approx. 40 of functions done in similar way that i want convert, this example is a very small one)

      //get filters from the filter table
      	function getFilter($filter = ''){
      		$select = "SELECT 
      					filter,
      					value, 
      					value_screen
      					FROM `filters` 
      					WHERE `filter` = '".$filter."' 
      					ORDER BY (value*1);
      					";				    
      		$query = mysql_query($select);
      		$result = array();
      		if (mysql_numrows($query) == 0) { 
      			$location = "Location: ".$modx->makeUrl(intval($modx->config['error_page']));
      			header($location);
      			exit();
      		} else {
      			while ($row = mysql_fetch_object($query)) {
      			$result[] = $row;
      			}
      		}
      		return $result;
      	}
        • 16551 ☆ A M B ☆
        • 54 Posts
        Just to make this clear: You don't have to use getObject, getCollection, etc.

        http://rtfm.modx.com/xpdo/2.x/class-reference/xpdo/xpdo.query
          • 18373 ☆ A M B ☆
          • 3,141 Posts
          Quote from: thapriest86 at Aug 28, 2013, 04:36 AM
          Example code of what i used to use, now want to "translate" to xPDO (there's approx. 40 of functions done in similar way that i want convert, this example is a very small one)

          //get filters from the filter table
          	function getFilter($filter = ''){
          		$select = "SELECT 
          					filter,
          					value, 
          					value_screen
          					FROM `filters` 
          					WHERE `filter` = '".$filter."' 
          					ORDER BY (value*1);
          					";				    
          		$query = mysql_query($select);
          		$result = array();
          		if (mysql_numrows($query) == 0) { 
          			$location = "Location: ".$modx->makeUrl(intval($modx->config['error_page']));
          			header($location);
          			exit();
          		} else {
          			while ($row = mysql_fetch_object($query)) {
          			$result[] = $row;
          			}
          		}
          		return $result;
          	}


          This is how I would translate that:
          function getFilter($filter = '') {
            $c = $modx->newQuery('Filter'); //assuming you set up the schema/model for it
            $c->where(array(
              'filter' => $filter,
            ));
            $c->sortby('value'); 
            
            $result = array();
            foreach ($modx->getIterator('Filter, $c) as $filter) {
              $result[] = $filter->toArray();
            }
            
            if (empty($result)) {
              $modx->sendRedirect($modx->makeUrl($modx->getOption('error_page')));
            }
            return $result;
          }
            Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

            Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
            • 40092
            • 265 Posts
            Thanks Mark, i am using that as a lead to learn from.

            Now i'm having some trouble with grabbing the $result on the frontend(Trying to get property of non-object in) So i'll be battling that first. smiley
              • 3749
              • 24,544 Posts
              Mark's version will give you $result as an array of arrays (so, no objects).

              To iterate through it, you'd use something like this:

              $filters = getFilter($filter);
              
              foreach($filters as $k => $fields) {
                 echo $fields['whatever'];
              }
              



              If you'd prefer to have them as objects you can just do this:

              function getFilter($filter = '') {
                $c = $modx->newQuery('Filter'); //assuming you set up the schema/model for it
                $c->where(array(
                  'filter' => $filter,
                ));
                $c->sortby('value'); 
                 
               
                $result = $modx->getIterator('Filter, $c);
                 
                if (empty($result)) {
                  $modx->sendRedirect($modx->makeUrl($modx->getOption('error_page')));
                }
                return $result;
              }


              Then, iterating through them would look something like this:

              $filters = getFilter($filter);
              foreach($filters as $filterObject) {
                 $fields = $filterObject->toArray();
                 echo $fields['whatever'];
              }


              You'd have to check the relative speed of using getIterator() versus getCollection(). For me, they seem to vary depending on the query and the size of the table. getIterator() will use less memory if that's an issue.
                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
                • 40092
                • 265 Posts
                Thank you guys! I'm really getting the hang of it now.

                One thing that i can't seem to accomplish though:

                Sorting by name and limiting the number of results


                It was due to an Innerjoin in wich both tables have the same column name.
                It just skips the limit and sorting, though? Is that a setting somewhere? [ed. note: thapriest86 last edited this post 13 years, 1 month ago.]
                  • 3749
                  • 24,544 Posts
                  You should be able to qualify the column name in your criteria: alias.columnName

                  Be sure your limit is an integer and your sortby is a string (may need to be qualified as above).
                    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
                    • 40092
                    • 265 Posts
                    Quote from: BobRay at Aug 29, 2013, 01:58 PM
                    You should be able to qualify the column name in your criteria: alias.columnName

                    Be sure your limit is an integer and your sortby is a string (may need to be qualified as above).

                    Got it! Thanks again.