/** * @param bool|true $safe * @return $this */ public function returnObjects($safe = true) { $cls = $this->metadata->getModelClass(); $dbColsAsKeys = array_flip($this->metadata->getDbColNames()); $this->rowDataMapper = function ($row) use($cls, $dbColsAsKeys, $safe) { if ($safe) { return new $cls(array_intersect_key($row, $dbColsAsKeys), false); } else { return new $cls($row, false); } }; return $this; }
/** * @param Metadata $metadata * @return Table */ public function metadataToTable(Metadata $metadata) { $tblName = $metadata->getDbTableName(); if (isset($this->md2tableCache[$tblName])) { return $this->md2tableCache[$tblName]; } $cols = []; foreach ($metadata->getLocalFields() as $fieldObj) { $col = $fieldObj->getDoctrineColumn(); $col->setLength($fieldObj->max_length); $col->setNotnull(!$fieldObj->null); $col->setComment($fieldObj->help_text); $col->setAutoincrement($fieldObj->auto_increment); $cols[] = $col; } $table = new Table($tblName, $cols); $this->md2tableCache[$tblName] = $table; foreach ($metadata->getLocalFields() as $fieldObj) { if ($fieldObj->unique) { $table->addUniqueIndex([$fieldObj->db_column]); } elseif ($fieldObj->db_index) { $table->addIndex([$fieldObj->db_column]); } if ($fieldObj->primary_key) { $table->setPrimaryKey([$fieldObj->db_column]); } if ($this->followRelations === true && $fieldObj instanceof ForeignKey) { $relationClass = $fieldObj->relationClass; $relationTable = $this->metadataToTable($relationClass::metadata()); $table->addForeignKeyConstraint($relationTable, [$fieldObj->db_column], [$fieldObj->to_field]); $this->generateQueue[] = $relationClass; } } if ($this->followRelations === true) { foreach ($metadata->getRelationFields() as $fieldObj) { if ($fieldObj instanceof ManyToMany) { if ($fieldObj->throughClass) { $throughClass = $fieldObj->throughClass; //$this->metadataToTable($throughClass::metadata()); $this->generateQueue[] = $throughClass; } } } } return $table; }
/** * todo: move outside, may be to helper function, or to form methods * @return array */ public function export() { $result = []; foreach ($this->metadata->getLocalFields() as $field) { if ($field->isRelation()) { $value = $field->viewValue($this->__get($field->name)); } else { $value = $field->viewValue($this->__get($field->db_column)); } $name = $field->verbose_name ? $field->verbose_name : implode(' ', array_map('ucfirst', explode('_', $field->name))); $result[$field->db_column] = ['name' => $name, 'value' => $value, 'choices' => $field->choices]; } return $result; }
/** * [$field, $lookupType, $column] * @param string $lookup * @return array * @throws \Exception */ protected function explainLookup($lookup) { $lookupArr = explode('__', $lookup); $lookupType = end($lookupArr); if ($this->lookuper->issetLookup($lookupType)) { $lookupType = array_pop($lookupArr); } else { $lookupType = 'exact'; } $lookup = implode('__', $lookupArr); $field = $this->metadata->findField($lookup); if ($this->metadata->hasFieldObj($field)) { return [$field, $lookupType, 't.' . $field->db_column]; } else { if (!isset($this->relatedSelectCols[$lookup])) { throw new \DomainException("Cant lookup for related field '{$lookup}' without selectRelated()"); } return [$field, $lookupType, $this->relatedSelectCols[$lookup]]; } }