/** * Returns an associative array of field definitions for this view. */ function &fields() { // check the cache first. if (isset($this->_cache[__FUNCTION__])) { return $this->_cache[__FUNCTION__]; } // it is not in the cache yet, let's calculate it. $out = array(); $data = $this->_parseSQL(); $this->_expandGlobs($data); $numCols = count($data['columns']); for ($i = 0; $i < $numCols; $i++) { $column = $data['columns'][$i]; if ($column['type'] == 'ident') { // This column is just a normal column that is drawn from a table. // vs. the alternative which is a function. if ($column['table']) { $table =& $this->getTableOrView($column['table']); } else { if ($column['alias']) { $key = $column['alias']; } else { $key = $column['value']; } $tablename = $this->guessColumnTableName($key); $table =& $this->getTableOrView($tablename); } $out[$key] =& $table->getField($key); unset($table); } else { $out[$column['alias']] = Dataface_Table::_newSchema($column['type'], $column['alias'], $this->name); } } $this->_cache[__FUNCTION__] =& $out; $this->_fields =& $out; return $out; }
/** * Adds a field to this table. * @param array $field A partial field definition. Must contain at least * Field (or name) and Type keys. * @param string $field[Field] The name of the field * @param string $field[Type] The type of the field (e.g. int(11)) * @param string $field[Default] The default value * @param string $field[Key] 'PRI' if this is part of the primary key. * @param string $field[Null] Empty if the field is not null. * @return array The finished field definition. */ function &addField($field) { if (!isset($field['Field']) and !isset($field['name'])) { $err = PEAR::raiseError("Attempt to add field that has no name."); return $err; } if (!isset($field['Field'])) { $field['Field'] = $field['name']; } if (!isset($field['name'])) { $field['name'] = $field['Field']; } $schema = Dataface_Table::_newSchema($field['Type'], $field['name'], $this->name); $fields =& $this->fields(); $fields[$field['name']] =& $schema; $conf = array(); $this->flattenConfigArray($field, $conf); foreach (array_keys($conf) as $key) { $this->setParameter($field['name'], $key, $conf[$key]); } return $conf; }