/** * Class constructor * * @param mixed $parent Parent record (Q\DB_Record), parent table (Q\DB_Record) or Database connection (Q\DB) * @param array $properties Field properties (can be passed as reference) * @param mixed $value */ protected function __construct($parent, $properties, $value = null) { parent::__construct($parent, $properties); if ($this->mode == self::MODE_ACTIVE) { $this->setValue($value); } }
/** * Cast a value according to the field type property or generate value for insert/update * * @param mixed $value * @param string $action 'insert', 'update' or NULL * @return mixed */ function castValue($value, $action = null) { if (isset($action) && ($this->_generateAction === 'both' || $this->_generateAction === $action)) { return $this->generate($value); } return parent::castValue($value, $action); }
/** * The class constructor. * * @param Q\DB_Result|Q\DB_Table $source * @param array $values Ordered or associated array with values */ protected function __construct($source = null, $values = null) { if (!isset($source)) { $fields = array(); foreach ((array) $values as $key => $value) { $this->_fieldIndex[$key] = array_push($this->_fields, DB_Field::create(null, array('name' => $key, 'table' => null, 'type' => gettype($value)), $value)) - 1; } } else { $this->_link = $source->getConnection(); $this->_baseTable = $source instanceof DB_Table ? $source : $source->getBasetable(); list($fields, $this->_fieldIndex, $this->_fieldnames, $this->_tablerefs) = $source->getInternalInfo(); if (!isset($values)) { foreach ($fields as $i => $fd) { $this->_fields[$i] = $fd->asNewActive($this); } } else { foreach ($fields as $i => $fd) { $this->_fields[$i] = $fd->asActive(isset($values[$fd->getName()]) ? $values[$fd->getName()] : (isset($values[$i]) ? $values[$i] : null), $this); } } } }
/** * Create fields based on the result. */ protected function initFields() { $this->fields = array(); if ($this->getBaseTable()) { $meta[null] = $this->getBaseTable()->getProperties(); $meta[$this->getBaseTable()->getTablename()] =& $meta[null]; } else { $meta[null] = array(); } foreach ($this->fetchFieldProperties() as $p) { list($props, $props_def) = $p; $tbl = !empty($props['table_db']) ? $props['table_db'] : null; if (!isset($meta[$tbl])) { $meta[$tbl] =& $this->link->getMetaData($tbl); } $fieldmeta = isset($meta[$tbl][$props['name_db']]) ? $meta[$tbl][$props['name_db']] : array(); // Apply alias settings if ($props['name'] != $props['name_db']) { unset($fieldmeta['description']); $key = strpos($props['name'], ':') !== false ? "#{$props['name']}" : "#alias:{$props['name']}"; if (isset($meta[null][$key])) { $fieldmeta = $meta[null][$key] + $fieldmeta; } } // Fix: Enum and set fields in query results are interpreted as string if ($props['type'] === 'char' && isset($fieldmeta['type']) && ($fieldmeta['type'] === 'enum' || $fieldmeta['type'] === 'set')) { unset($props['type']); } $props = $props + $fieldmeta + $props_def; if (!isset($props['table_def'])) { $this->link->applyFieldDefaults($props); } $this->fields[] = DB_Field::create($this, $props); } $this->refreshFieldIndex(); }
/** * Create DB_Fields based on the supplied metadata * * @param array $metadata */ protected function createFields($metadata) { foreach ($metadata as $name => $properties) { if ($name[0] == '#') { continue; } $field = DB_Field::create($this, $properties); $this->_fieldindex[$name] = $field; if ($properties['db_name']) { $this->_fields[] = $field; } } $this->reindex(); }