SwatDB/SwatDB.php
public static function getGroupedOptionArray(
        $db,
        $table,
        $title_field,
        $id_field,
        $group_table,
        $group_title_field,
        $group_id_field,
        $group_field,
        $order_by_clause = null,
        $where_clause = null,
        $tree = null
    ) {
        $title_field = new SwatDBField($title_field, 'text');
        $id_field = new SwatDBField($id_field, 'integer');
        $group_title_field = new SwatDBField($group_title_field, 'text');
        $group_id_field = new SwatDBField($group_id_field, 'integer');
        $group_field = new SwatDBField($group_field, 'text');

        $sql = 'select %s as id, %s as title, %s as group_title, %s as group_id
			from %s';

        $sql = sprintf(
            $sql,
            "{$table}.{$id_field->name}",
            "{$table}.{$title_field->name}",
            "{$group_table}.{$group_title_field->name}",
            "{$group_table}.{$group_id_field->name}",
            $table
        );

        $sql .= ' inner join %s on %s = %s';
        $sql = sprintf(
            $sql,
            $group_table,
            "{$group_table}.{$group_id_field->name}",
            "{$table}.{$group_field->name}"
        );

        if ($where_clause != null) {
            $sql .= ' where ' . $where_clause;
        }

        if ($order_by_clause != null) {
            $sql .= ' order by ' . $order_by_clause;
        }

        $rs = self::query($db, $sql, null);

        $options = array();

        if ($tree !== null && $tree instanceof SwatDataTreeNode) {
            $base_parent = $tree;
        } else {
            $base_parent = new SwatDataTreeNode(null, Swat::_('Root'));
        }

        $current_group = null;

        $row = $rs->fetchRow(MDB2_FETCHMODE_OBJECT);
        while ($row) {
            if ($current_group !== $row->group_id) {
                $current_parent = new SwatDataTreeNode(null, $row->group_title);
                $base_parent->addChild($current_parent);
                $current_group = $row->group_id;
            }

            $current_parent->addChild(
                new SwatDataTreeNode($row->id, $row->title)
            );

            $row = $rs->fetchRow(MDB2_FETCHMODE_OBJECT);
        }

        return $base_parent;
    }
public static function getGroupedOptionArray(
        $db,
        $table,
        $title_field,
        $id_field,
        $group_table,
        $group_title_field,
        $group_id_field,
        $group_field,
        $order_by_clause = null,
        $where_clause = null,
        $tree = null
    ) {
        $title_field = new SwatDBField($title_field, 'text');
        $id_field = new SwatDBField($id_field, 'integer');
        $group_title_field = new SwatDBField($group_title_field, 'text');
        $group_id_field = new SwatDBField($group_id_field, 'integer');
        $group_field = new SwatDBField($group_field, 'text');

        $sql = 'select %s as id, %s as title, %s as group_title, %s as group_id
			from %s';

        $sql = sprintf(
            $sql,
            "{$table}.{$id_field->name}",
            "{$table}.{$title_field->name}",
            "{$group_table}.{$group_title_field->name}",
            "{$group_table}.{$group_id_field->name}",
            $table
        );

        $sql .= ' inner join %s on %s = %s';
        $sql = sprintf(
            $sql,
            $group_table,
            "{$group_table}.{$group_id_field->name}",
            "{$table}.{$group_field->name}"
        );

        if ($where_clause != null) {
            $sql .= ' where ' . $where_clause;
        }

        if ($order_by_clause != null) {
            $sql .= ' order by ' . $order_by_clause;
        }

        $rs = self::query($db, $sql, null);

        $options = array();

        if ($tree !== null && $tree instanceof SwatDataTreeNode) {
            $base_parent = $tree;
        } else {
            $base_parent = new SwatDataTreeNode(null, Swat::_('Root'));
        }

        $current_group = null;

        $row = $rs->fetchRow(MDB2_FETCHMODE_OBJECT);
        while ($row) {
            if ($current_group !== $row->group_id) {
                $current_parent = new SwatDataTreeNode(null, $row->group_title);
                $base_parent->addChild($current_parent);
                $current_group = $row->group_id;
            }

            $current_parent->addChild(
                new SwatDataTreeNode($row->id, $row->title)
            );

            $row = $rs->fetchRow(MDB2_FETCHMODE_OBJECT);
        }

        return $base_parent;
    }
public static function getGroupedOptionArray ( $ db , $ table , $ title_field , $ id_field , $ group_table , $ group_title_field , $ group_id_field , $ group_field , $ order_by_clause = null , $ where_clause = null , $ tree = null ) { $ title_field = new SwatDBField ( $ title_field , 'text' ) ; $ id_field = new SwatDBField ( $ id_field , 'integer' ) ; $ group_title_field = new SwatDBField ( $ group_title_field , 'text' ) ; $ group_id_field = new SwatDBField ( $ group_id_field , 'integer' ) ; $ group_field = new SwatDBField ( $ group_field , 'text' ) ; $ sql = 'select %s as id, %s as title, %s as group_title, %s as group_id
			from %s' ; $ sql = sprintf ( $ sql , "{$table}.{$id_field->name}" , "{$table}.{$title_field->name}" , "{$group_table}.{$group_title_field->name}" , "{$group_table}.{$group_id_field->name}" , $ table ) ; $ sql .= ' inner join %s on %s = %s' ; $ sql = sprintf ( $ sql , $ group_table , "{$group_table}.{$group_id_field->name}" , "{$table}.{$group_field->name}" ) ; if ( $ where_clause != null ) { $ sql .= ' where ' . $ where_clause ; } if ( $ order_by_clause != null ) { $ sql .= ' order by ' . $ order_by_clause ; } $ rs = self :: query ( $ db , $ sql , null ) ; $ options = array ( ) ; if ( $ tree !== null && $ tree instanceof SwatDataTreeNode ) { $ base_parent = $ tree ; } else { $ base_parent = new SwatDataTreeNode ( null , Swat :: _ ( 'Root' ) ) ; } $ current_group = null ; $ row = $ rs -> fetchRow ( MDB2_FETCHMODE_OBJECT ) ; while ( $ row ) { if ( $ current_group !== $ row -> group_id ) { $ current_parent = new SwatDataTreeNode ( null , $ row -> group_title ) ; $ base_parent -> addChild ( $ current_parent ) ; $ current_group = $ row -> group_id ; } $ current_parent -> addChild ( new SwatDataTreeNode ( $ row -> id , $ row -> title ) ) ; $ row = $ rs -> fetchRow ( MDB2_FETCHMODE_OBJECT ) ; } return $ base_parent ; }
Queries for a grouped option array

Convenience method to query a grouped list of {@link SwatDataTreeNode}
objects used for things like {@link SwatCheckboxList} where checkboxes
are grouped together under a title.

@param MDB2_Driver_Common $db The database connection.

@param string $table The database table to query.

@param string $title_field The name of the database field to query for
the title. Can be given in the form type:name where type is a
standard MDB2 datatype. If type is ommitted, then text is
assummed for this field.

@param string $id_field The name of the database field to query for
the id. Can be given in the form type:name where type is a
standard MDB2 datatype. If type is ommitted, then integer is
assummed for this field.

@param string $group_table The database table that the group titles come
from.

@param string $group_idfield The name of the database field to query for
the id of the $group_table. Can be given in the form type:name
where type is a standard MDB2 datatype. If type is ommitted, then
integer is assummed for this field.

@param string $group_title_field The name of the database field to query
for the group title. Can be given in the form type:name where
type is a standard MDB2 datatype. If type is ommitted, then text
is assummed for this field.

@param string $group_field The name of the database field in $table that
links with the $group_idfield. Can be given in the form type:name
where type is a standard MDB2 datatype. If type is ommitted, then
integer is assummed for this field.

@param string $order_by_clause Optional comma deliminated list of
database field names to use in the <i>order by</i> clause.
Do not include "order by" in the string; only include the list
of field names. Pass null to skip over this paramater.

@param string $where_clause Optional <i>where</i> clause to limit the
returned results.  Do not include "where" in the string; only
include the conditionals.
@param SwatDataTreeNode $tree a tree to add nodes to. If no tree is
specified, nodes are added to a new
empty tree.

@return SwatDataTreeNode a tree composed of {@link SwatDataTreeNode}
objects.

@throws SwatDBException
Queries for a grouped option array