Adding Order By to Views

Events happening in the community are now at Drupal community events on www.drupal.org.
Posted by andrew1056@drup... on February 2, 2009 at 9:41pm

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

Posted by dwees on February 2, 2009 at 11:45pm

You might find this conversation useful.

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

Thanks!

Posted by andrew1056@drup... on February 3, 2009 at 1:42am

Works for me. Thanks for the link.

I don't suppose anyone would

Posted by andrew1056@drup... on February 3, 2009 at 2:27pm

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()

Posted by ximo on July 19, 2009 at 3:10pm

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:

AltStyle によって変換されたページ (->オリジナル) /