Skip to content

Commit

Permalink
Some tweaks to buildSortingSQL. Should result in 25% improvements whe…
Browse files Browse the repository at this point in the history
…n sorting using Checkbox, Date, Input or Upload fields. RE: #657
  • Loading branch information
Brendan Abbott committed Jun 17, 2011
1 parent 7e473b0 commit 6a18bba
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 17 deletions.
9 changes: 7 additions & 2 deletions symphony/lib/toolkit/class.field.php
Original file line number Diff line number Diff line change
Expand Up @@ -984,8 +984,13 @@ public function buildDSRetrievalSQL($data, &$joins, &$where, $andOperation = fal
* is declared either 'random' or 'rand' then a random sort is applied.
*/
public function buildSortingSQL(&$joins, &$where, &$sort, $order='ASC'){
$joins .= "LEFT OUTER JOIN `tbl_entries_data_".$this->get('id')."` AS `ed` ON (`e`.`id` = `ed`.`entry_id`) ";
$sort = 'ORDER BY ' . (in_array(strtolower($order), array('random', 'rand')) ? 'RAND()' : "`ed`.`value` $order");
if(in_array(strtolower($order), array('random', 'rand'))) {
$sort = 'ORDER BY RAND()';
}
else {
$joins .= "LEFT OUTER JOIN `tbl_entries_data_".$this->get('id')."` AS `ed` ON (`e`.`id` = `ed`.`entry_id`) ";
$sort = 'ORDER BY `ed`.`value` ' . $order;
}
}

/**
Expand Down
17 changes: 10 additions & 7 deletions symphony/lib/toolkit/fields/field.author.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,16 @@ public function buildDSRetrievalSQL($data, &$joins, &$where, $andOperation = fal
-------------------------------------------------------------------------*/

public function buildSortingSQL(&$joins, &$where, &$sort, $order='ASC'){
$joins .= "
LEFT OUTER JOIN `tbl_entries_data_".$this->get('id')."` AS `ed` ON (`e`.`id` = `ed`.`entry_id`)
JOIN `tbl_authors` AS `a` ON (ed.author_id = a.id)
";
$sort = 'ORDER BY ' . (in_array(strtolower($order), array('random', 'rand'))
? 'RAND()'
: "`a`.`first_name` " . $order . ", `a`.`last_name` " . $order);
if(in_array(strtolower($order), array('random', 'rand'))) {
$sort = 'ORDER BY RAND()';
}
else {
$joins .= "
LEFT OUTER JOIN `tbl_entries_data_".$this->get('id')."` AS `ed` ON (`e`.`id` = `ed`.`entry_id`)
JOIN `tbl_authors` AS `a` ON (ed.author_id = a.id)
";
$sort = "ORDER BY `a`.`first_name` " . $order . ", `a`.`last_name` " . $order;
}
}

/*-------------------------------------------------------------------------
Expand Down
17 changes: 15 additions & 2 deletions symphony/lib/toolkit/fields/field.checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,21 @@ public function buildDSRetrievalSQL($data, &$joins, &$where, $andOperation = fal
-------------------------------------------------------------------------*/

public function buildSortingSQL(&$joins, &$where, &$sort, $order='ASC'){
$joins .= "LEFT OUTER JOIN `tbl_entries_data_".$this->get('id')."` AS `ed` ON (`e`.`id` = `ed`.`entry_id`) ";
$sort = 'ORDER BY ' . (in_array(strtolower($order), array('random', 'rand')) ? 'RAND()' : "`ed`.`value` $order");
if(in_array(strtolower($order), array('random', 'rand'))) {
$sort = 'ORDER BY RAND()';
}
else {
$sort = sprintf(
'ORDER BY (
SELECT %s
FROM tbl_entries_data_%d AS `ed`
WHERE entry_id = e.id
) %s',
'`ed`.value',
$this->get('id'),
$order
);
}
}

/*-------------------------------------------------------------------------
Expand Down
17 changes: 15 additions & 2 deletions symphony/lib/toolkit/fields/field.date.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,21 @@ public function buildDSRetrievalSQL($data, &$joins, &$where, $andOperation=false
-------------------------------------------------------------------------*/

public function buildSortingSQL(&$joins, &$where, &$sort, $order='ASC') {
$joins .= "LEFT OUTER JOIN `tbl_entries_data_".$this->get('id')."` AS `ed` ON (`e`.`id` = `ed`.`entry_id`) ";
$sort = 'ORDER BY ' . (in_array(strtolower($order), array('random', 'rand')) ? 'RAND()' : "`ed`.`gmt` $order");
if(in_array(strtolower($order), array('random', 'rand'))) {
$sort = 'ORDER BY RAND()';
}
else {
$sort = sprintf(
'ORDER BY (
SELECT %s
FROM tbl_entries_data_%d AS `ed`
WHERE entry_id = e.id
) %s',
'`ed`.gmt',
$this->get('id'),
$order
);
}
}

/*-------------------------------------------------------------------------
Expand Down
17 changes: 15 additions & 2 deletions symphony/lib/toolkit/fields/field.input.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,21 @@ public function buildDSRetrievalSQL($data, &$joins, &$where, $andOperation = fal
-------------------------------------------------------------------------*/

public function buildSortingSQL(&$joins, &$where, &$sort, $order='ASC'){
$joins .= "LEFT OUTER JOIN `tbl_entries_data_".$this->get('id')."` AS `ed` ON (`e`.`id` = `ed`.`entry_id`) ";
$sort = 'ORDER BY ' . (in_array(strtolower($order), array('random', 'rand')) ? 'RAND()' : "`ed`.`value` $order");
if(in_array(strtolower($order), array('random', 'rand'))) {
$sort = 'ORDER BY RAND()';
}
else {
$sort = sprintf(
'ORDER BY (
SELECT %s
FROM tbl_entries_data_%d AS `ed`
WHERE entry_id = e.id
) %s',
'`ed`.value',
$this->get('id'),
$order
);
}
}

/*-------------------------------------------------------------------------
Expand Down
17 changes: 15 additions & 2 deletions symphony/lib/toolkit/fields/field.upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -575,8 +575,21 @@ public function buildDSRetrievalSQL($data, &$joins, &$where, $andOperation = fal
-------------------------------------------------------------------------*/

public function buildSortingSQL(&$joins, &$where, &$sort, $order='ASC'){
$joins .= "LEFT OUTER JOIN `tbl_entries_data_".$this->get('id')."` AS `ed` ON (`e`.`id` = `ed`.`entry_id`) ";
$sort = 'ORDER BY ' . (in_array(strtolower($order), array('random', 'rand')) ? 'RAND()' : "`ed`.`file` $order");
if(in_array(strtolower($order), array('random', 'rand'))) {
$sort = 'ORDER BY RAND()';
}
else {
$sort = sprintf(
'ORDER BY (
SELECT %s
FROM tbl_entries_data_%d AS `ed`
WHERE entry_id = e.id
) %s',
'`ed`.file',
$this->get('id'),
$order
);
}
}

/*-------------------------------------------------------------------------
Expand Down

0 comments on commit 6a18bba

Please sign in to comment.