/** * Create a record for existing data. * @param array $data Associative array of record data. * @param ReadSelectionBuilder $selection The selection that led to the creation of * this record. * @return RecordBuilder A record. */ public function createExisting($data = array(), ReadSelectionBuilder $selection) { $additonal = $selection->additionalFields; if (empty($additonal)) { return RecordBuilder::createExisting($this, $data, array()); } $virtual = array(); $subrecords = array(); foreach ($raw as $field => $value) { if (isset($addtional[$field])) { if (isset($additional[$field]['record'])) { $record = $additional[$field]['record']; if (!isset($subrecords[$record])) { $subrecords[$record] = array('model' => $additional[$field]['model'], 'null' => true, 'data' => array()); } $subrecords[$record]['data'][$additional[$field]['recordField']] = $value; if (isset($value)) { $subrecords[$record]['null'] = false; } } else { $virtual[$field] = $value; } unset($data[$field]); } } foreach ($subrecords as $field => $record) { if ($record['null']) { $virtual[$field] = null; } else { $virtual[$field] = RecordBuilder::createExisting($record['model'], $record['data']); } } return RecordBuilder::createExisting($this, $data, $virtual); }
/** * Convert a record to an ActiveRecord. * @param RecordBuilder $record Record. * @return ActiveRecord Active record. */ public function convert(RecordBuilder $record) { return ActiveRecord::createExisting($this, $record->getData(), $record->getVirtualData(), $this->record); }
/** * {@inheritdoc} */ public function createExisting($raw = array(), ReadSelectionBuilder $selection) { $typeAdapter = $this->owner->getTypeAdapter(); $additional = $selection->additionalFields; $data = array(); $virtual = array(); $subrecords = array(); if ($this->caseInsensitive) { $lower = array(); foreach ($this->getFields() as $field) { $lower[strtolower($field)] = $field; } foreach ($additional as $field => $a) { $lower[strtolower($field)] = $field; } } foreach ($raw as $field => $value) { if (isset($lower) and isset($lower[$field])) { $field = $lower[$field]; } if (isset($additional[$field])) { if (isset($additional[$field]['type'])) { $value = $typeAdapter->decode($additional[$field]['type'], $value); } if (isset($additional[$field]['record'])) { $record = $additional[$field]['record']; if (!isset($subrecords[$record])) { $subrecords[$record] = array('model' => $additional[$field]['model'], 'null' => true, 'data' => array()); } $subrecords[$record]['data'][$additional[$field]['recordField']] = $value; if (isset($value)) { $subrecords[$record]['null'] = false; } } else { $virtual[$field] = $value; } } else { $type = $this->getType($field); if (!isset($type)) { throw new QueryException(tr('Schema %1 does not contain field %2', $this->getName(), $field)); } $data[$field] = $typeAdapter->decode($this->getType($field), $value); } } foreach ($subrecords as $field => $record) { if ($record['null']) { $virtual[$field] = null; } else { $virtual[$field] = RecordBuilder::createExisting($record['model'], $record['data']); } } return RecordBuilder::createExisting($this, $data, $virtual); }