We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34109
    • 119 Posts
    I'm trying to perform a division on two columns in a table. My code is as below:

    $c = $modx->newQuery('starRating');
    $c->select('vote_total/vote_count AS rating, star_id');
    $c->sortby('rating','ASC');
    $c->prepare();
    echo ($c->toSql());
    
    $ratings = $modx->getCollection('starRating', $c);
    foreach ($ratings as $rating) {
       $output.= $rating->get('star_id').',';
    }
    


    This gives a sql query of

     SELECT vote_total/vote_count AS rating, `star_id` FROM `modx_star_rating` AS `starRating` ORDER BY rating ASC


    but I get an out of memory error. What am I doing wrong? It's based on the css star rating extra.

    Cheers,

    Chris
      Studio Republic
      http://www.studiorepublic.com
      0845 226 3205
      @christodhunter
      • 34109
      • 119 Posts
      What an idiot! Need to select the ID column for getCollection to work. As you were!
        Studio Republic
        http://www.studiorepublic.com
        0845 226 3205
        @christodhunter
        • 34012
        • 88 Posts
        Yup, you are right there.

        When using getCollection (especially getCollection) always select the primary key field(s) for the query.

        The data is stored as object collection by primary key value. So if the key is missing from the query, you'll end up in knee deep you know what.

        Just a reminded for all you there smiley
          Almost retired from web-development industry but still randomly writing at Lazylegs.info and on schedule hopefully in near future to finish Oracle and PostgreSQL ports of MODX