00001 <?php 00008 class DbSelectField extends SelectField 00009 { 00016 function __construct($name, &$parentForm, array $attribs = array()) 00017 { 00018 if(!array_key_exists('query', $attribs)) 00019 throw new Exception('Query not set'); 00020 00021 $query = $attribs['query']; 00022 unset($attribs['query']); 00023 00024 $attribs['options'] = array(); 00025 parent::__construct($name, $parentForm, $attribs); 00026 $this->options = $this->queryToValues($query); 00027 } 00028 00033 function queryToValues($query) 00034 { 00035 $dbConn = $this->parentForm->dbConn; 00036 $res =& $dbConn->query($query); 00037 $ret = array(); 00038 00039 while($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) 00040 { 00041 if(!array_key_exists('value', $row) || !array_key_exists('title', $row)) 00042 throw new InvalidQueryException("Query did not include columns 'value' & 'title'"); 00043 00044 $ret[$row['value']] = $row['title']; 00045 } 00046 00047 return $ret; 00048 } 00049 } 00050 ?>