We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34120
    • 236 Posts
    Hi, I’m hoping someone can help me out with writing a query. I’ve successfully added the package but when it comes to the query I can’t get anything working. I’m pretty new to this xpdo business so can you tell me is there anything glaringly wrong with the query below?

    	$c = $modx->newQuery('Products');
    	$c->where(array('product_groups.category_id' => $categoryId));	
    	$c->andCondition(array('product_groups.product_group_id' => $productGroupId));
    	$c->sortby('product_groups.product_group_id','ASC');
    
    	$products = $modx->getCollection('Products',$c);
    	foreach($products as $product) {
    		$output = 'Image: ' . $product->get('group_image');
    	}
    	echo $output;

    Many thanks
      • 22303 MODX Staff
      • 10,725 Posts
      What is product_groups?
        • 34120
        • 236 Posts
        product_groups is the name of the database table, I’m guessing that’s incorrect, I’m still thinking in sql mode.
          • 22303 MODX Staff
          • 10,725 Posts
          Quote from: rf9 at Jun 21, 2011, 04:55 PM

          product_groups is the name of the database table, I’m guessing that’s incorrect, I’m still thinking in sql mode.
          Right—xPDO uses the class names and aliases (for relationships) that you define in your schema. So in this case it should be something like this (assuming product_groups is the table name of the Products class):

          <?php
          $output = array();
          $c = $modx->newQuery('Products');
          $c->where(array(
              'category_id' => $categoryId,
              'product_group_id' => $productGroupId
          ));
          $c->sortby('product_group_id','ASC');
          
          $products = $modx->getCollection('Products', $c);
          foreach($products as $product) {
              $output[] = 'Image: ' . $product->get('group_image');
          }
          return implode("\n", $output);
          
            • 34120
            • 236 Posts
            Thanks so much for this, I think I’m getting the grasp of it now.
            Aside from the class issue, one thing that has caused me problems was not adding the table prefix in addPackage. Once I added this I managed to get things working.

            This bit of the documentation threw me a bit:
            Using the table_prefix is not recommended unless you have a good reason to specifically set the table prefix.

            http://rtfm.modx.com/display/xPDO20/Loading+Packages

            All is working now though, thanks for your time.