We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40092
    • 265 Posts
    Hi Folks,

    Due to a whole bunch of things i've been sideways of modx for a while, but i'm back on track and trying to get my projects i was working on up again.

    One of these projects is rewriting an evolution website(backend) to Revolution. (what better way is there to learn, right?). However, in this evolution website they use a custom class with several functions that return values to the frontend.

    For example this is such a function:
    function getTeaserValue() {
    		$select = "SELECT * FROM teaser";
    		$query = mysql_query($select);
    		$value = mysql_fetch_array($query);
    
    		return $value;

    And in the frontend they use it like so:
    $teaser = new nameofcustomclass();
    $value = $teaser->getTeaserValue(); 
    
    <a href="<?php echo $value['linklocation']?>" title="<?php echo $value['value']?>"><?php echo $value['value']?></a>
    
    


    It's (simply) passing on an array, right?

    I figured, i'd rewrite it to:
    function getTeaserValue() {
    		global $modx;
    		$select = $modx->newQuery('Teaser');
    		$value = $modx->getCollection('Teaser', $select);
    		
    		
    			return $value;
    			
    	}
    


    But that ship doesn't sail. What am i doing wrong or maybe i could do better? (it's a class of approx. 900 lines code, i really want to understand what i'm doing smiley )

      • 40092
      • 265 Posts
      Found it, or at least, this worked.
      function getTeaserValue() {
      		global $modx;
      		$select = $modx->newQuery('Teaser');
      		$value = $modx->getCollection('Teaser', $select);
      		
      		foreach($values as $value){
      			$output = $value->toArray();
      			}
      			return $output;
      			
      	}
      


      Or is there a better way?

      Additional question(s) arose when i was working on the file.

      Anyone knows how to make a BETWEEN condition, and how to do something like tabel.name as name ?
      I've treid to find it in the docs, but i can;t find it.

        • 18373 ☆ A M B ☆
        • 3,141 Posts
        $select = $modx->newQuery('Teaser');
        $select->where(array(
          'field:>=' => 500,
          'AND:field:<=' => 750,
        ));
        $select->select(array(
          'name' => 'Teaser.name',
        ));
        $teasers = $modx->getCollection('Teaser', $select);



        http://rtfm.modx.com/xpdo/2.x/getting-started/using-your-xpdo-model/retrieving-objects
          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
          Okay, so this will surely work, however i am not exactly sure what it all means.

          [ed. note: thapriest86 last edited this post 13 years, 1 month ago.]
            • 40092
            • 265 Posts
            Okay, i think i got this covered now. I now have a fair understanding of constructing queries like this.

            However, i might have the stupidest question ever, but my head is boiling right now.

            In the old way we used

            	while ($row = mysql_fetch_object($query)) {
            			
            				foreach ($row as $key => $value) {
            //does utf_8 decode
            $row->$key = $this->cleanfromdatabase($row->$key);
            }
            				
            			$result[] = $row;
            			}
            		}
            		return $result;
            	}
            


            To fetch the resultset, however, in the new way (i translated a bunch of statements to PDO ones, i'd thought to simply replace $row by $query but that gave me the error:

            Attempt to assign property of non-object in line 322

            Wich is this line:
            $row->$key = $this->cleanfromdatabase($row->$key);
            


            Am i overlooking the way getCollection and fetc_object fetch the resultset? Or something else?
              • 3749
              • 24,544 Posts
              Yes, getCollection() returns an array of objects (in your case, Teaser objects), so you can iterate through them with something like this:


              foreach($teasers as $teaser) {
                  $name = $teaser->get('name');
                  $output .= $name;
              }
              
              return $output;



              If the Teaser class extends xPDOObject or xPDOSimpleObject, you can also do this to get an associative array of the object's fields:

              $fields = $teaser->toArray();

                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
                Thanks all, learned a lot again today.

                @Bob The iteration i already knew how to do, but in this case there is a whole bunch of columns to iterate, depending on several conditional AND/OR statements. (price-filters, rating, location etc. ).

                I'm beginning to think that with PDO, you can't just pick it all up like i did with the mysql_fetch_object, but have to iterate every single column?

                Regards,
                Robin
                  • 3749
                  • 24,544 Posts
                  You have all the rows (as objects) and if you want each of them as an array you can use toArray().

                  $fields = $teaser->toArray();


                  If you dump that, you can probably figure out how to access the fields you want.

                  BTW, you can always use PDO (rather than xPDO) and preserve most of your previous code.
                    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
                    I'll check that tommorow, thanks for the pointers. smiley

                    I know i can preserve most of the code, in a second i can have it all back to work. Point of this is exercise, i want to learn what PDO can do and what i can do with it. smiley
                      • 3749
                      • 24,544 Posts
                      FYI PDO != xPDO. wink

                      xPDO was created by MODX's Jason Coward (OpenGeek) and is used extensively in MODX.

                      Your code above, and all the $modx->get*() methods are xPDO.

                      PDO functions usually start with PDO:: and are very rarely used in the MODX core.
                        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