1
bingomanatee
Complete Criteria Class

One thing I noticed about the Criteria class suite is that it has incomplete support for the order and limit functions of SQL. It has properties for these syntax elements but they don't render.

-- SAMPLE DELETED BY AUTHOR --

2
bingomanatee
Re: Complete Criteria Class

I originally modified the Criteria objects before fully understanding their implmentation in XOOPS. Since there are rationale (if not an absolute requirement) for separating the limit and sort fields in XOOPS you can't really do that.

So I have written an extension class for CriteriaCompo that effects the same thing. Note that I am working in PHP 5.0 and so have not tested this on 4.x platforms.

Note also that this will cascade functionally to renderWhere() and renderLdap() execution. As I am not Ldap-educated I can't really say how appropriate this is.


/*********************
 * 
 * The classic Criteria classes are designed to split the load 
 * of the SQL generation with the calling code; specifically leveraging off 
 * of the db_query's limit oriented parameters. 
 * Criteria_sort_limit and Criteria_sort_limit_compo 
 * generates all the required SQL for the selection. 
 *
 */

require_once('criteria.php');


class 
Criteria_sort_limit extends Criteria 
{
  function 
render()
  {
    
$ret parent::render();
    
    if (
$this->getSort())
    {
        
$ret .= " ORDER BY {$this->getSort()} {$this->getOrder()} ";
    }
    
    if (
$this->getLimit())
    {
        if (
$this->getStart())
        {
            
$ret .= " LIMIT {$this->getLimit()} OFFSET {$this->getStart()} ";
        }
        else 
        {
            
$ret .= " LIMIT {$this->getLimit()} ";            
        }
    }
    else
    {
        
// mySQL syntax is not amenable to an unlimited start offset
    
}
    
    return 
$ret;
  }  
}

class 
Criteria_sort_limit_compo extends CriteriaCompo 
{
  function 
render()
  {
    
$ret parent::render();
    
    if (
$this->getSort())
    {
        
$ret .= " ORDER BY {$this->getSort()} {$this->getOrder()} ";
    }
    
    if (
$this->getLimit())
    {
        if (
$this->getStart())
        {
            
$ret .= " LIMIT {$this->getLimit()} OFFSET {$this->getStart()} ";
        }
        else 
        {
            
$ret .= " LIMIT {$this->getLimit()} ";            
        }
    }
    else
    {
        
// mySQL syntax is not amenable to an unlimited start offset
    
}
    
    return 
$ret;
  }
}

?>

3
phppp
Re: Complete Criteria Class
  • 2006/9/12 18:38

  • phppp

  • XOOPS Contributor

  • Posts: 2857

  • Since: 2004/1/25



4
Dave_L
Re: Complete Criteria Class
  • 2006/9/12 19:59

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


Since this subject has already been brought up here, I'll add another comment.

Another restriction of the Critera class is that it doesn't allow multiple specifications for the ORDER BY clause. For example: "ORDER BY col1 ASC, col2 DESC"

Maybe bingomanatee's patch addresses that, but it doesn't look like it.

This could be handled by changing

var $order = 'ASC';
var $sort = '';

to an array:

var $order_by = array();

The elements of the array would each be two-element arrays. The example "ORDER BY col1 ASC, col2 DESC" would be represented as:

$order_by = array(
array('col_name' => 'col1', 'asc' => true),
array('col_name' => 'col2', 'asc' => false),
);

If the new variable $order_by is added, and the old variables $order and $sort kept, downward-compatibility could be achieved.

Login

Who's Online

445 user(s) are online (366 user(s) are browsing Support Forums)


Members: 0


Guests: 445


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits