Adding Order By to Views

Events happening in the community are now at Drupal community events on www.drupal.org.
andrew1056@drupal.org's picture

I'm working on the uc_auction.module and have come up with corrected views code that looks like this:

class uc_auction_handler_field_high_bid extends views_handler_field {
    function query() {
     $join = new views_join();
      $join->table = 'uc_auction_bids';
     $join->field = 'bid';
     $join->left_table = 'uc_auction';
     $join->left_field = 'high_bid';
       $join->type = 'LEFT';
     $this->query->queue_table('uc_auction_bids', NULL, $join);
    
       $this->query->add_field('uc_auction', 'start_price');
        $this->query->add_field('uc_auction_bids', 'amount');
    }
 
   function pre_render($values) {
     $this->field_alias = $values[0]->uc_auction_bids_amount == NULL ? 'uc_auction_bids_amount' : 'uc_auction_start_price';
   }
}

The problem is that when I go to sort it, it gets sorted on high_bid, but I want to sort it on uc_auction_bids_amount.

So, not being able to find too much views examples of this I have come up with this:

class uc_auction_handler_sort_high_bid extends views_handler_sort {
  function query(){
      $this->query->add_orderby('uc_auction_bids', 'uc_auction_bids_amount', $this->options['order']);

   }
}

I know this isn't right because it doesn't work, but if somebody could point me in the right direction it would be a huge help.

Comments

You might find this

dwees's picture

You might find this conversation useful.

http://groups.drupal.org/node/18277

Thanks!

andrew1056@drupal.org's picture

Works for me. Thanks for the link.

I don't suppose anyone would

andrew1056@drupal.org's picture

I don't suppose anyone would know how to do a COUNT/GROUP BY in views? Here's the query:

SELECT node.nid AS nid,
   node.type AS node_type,
   COUNT(uc_auction_bids.amount) AS uc_auction_bids_count,
   uc_auction.expiry AS uc_auction_expiry
FROM node node
INNER JOIN uc_auction uc_auction ON node.nid = uc_auction.nid
LEFT JOIN uc_auction_bids uc_auction_bids ON uc_auction.high_bid = uc_auction_bids.bid
WHERE (node.status <> 0) AND (node.type IN ('product'))
   GROUP BY nid ASC

hook_views_query_alter()

ximo's picture

Probably not the best way to do it, but it works and it's easy:

<?php
/**
* Implementation of hook_views_query_alter().
*/
function mymodule_views_query_alter(&$view, &$query) {
  if (
$view->name == 'my_view') {

   
// Do whatever you want with the orderby array.
   
$query->orderby[] = "my_field_alias ASC";

   
// If you have Devel module installed (you should), do a DPM.
   
dpm($query);

  }
}
?>

Joakim Stai - NodeOne

Views Developers

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: