public function getBelongsToReference($table, $key) { $tableColumns = $this->structure->getBelongsToReference($table); foreach ($tableColumns as $column => $targetTable) { if (stripos($column, $key) !== FALSE) { return array($targetTable, $column); } } if ($this->structure->isRebuilt()) { return NULL; } $this->structure->rebuild(); return $this->getBelongsToReference($table, $key); }
public function getBelongsToReference($table, $key) { $tableColumns = $this->structure->getBelongsToReference($table); foreach ($tableColumns as $column => $targetTable) { if (stripos($column, $key) !== FALSE || stripos($targetTable, $key) !== FALSE) { //also check if any reference exists return [$targetTable, $column]; } } if ($this->structure->isRebuilt()) { return NULL; } $this->structure->rebuild(); return $this->getBelongsToReference($table, $key); }
private function getCachedTableList() { if (!$this->cacheTableList) { $this->cacheTableList = array_flip(array_map(function ($pair) { return isset($pair['fullName']) ? $pair['fullName'] : $pair['name']; }, $this->structure->getTables())); } return $this->cacheTableList; }
protected function findManyHasManyPrimaryColumns($joinTable, $sourceTable, $targetTable) { $useFQN = strpos($sourceTable, '.') !== FALSE; $keys = $this->databaseStructure->getBelongsToReference($joinTable); foreach ($keys as $column => $table) { $table = $useFQN ? $table : preg_replace('#^(.*\\.)?(.*)$#', '$2', $table); if ($table === $sourceTable) { $sourceId = $column; } elseif ($table === $targetTable) { $targetId = $column; } } if (!isset($sourceId, $targetId)) { throw new InvalidStateException("No primary keys detected for many has many '{$joinTable}' join table."); } return [$sourceId, $targetId]; }
public function persist(IEntity $entity) { if (!$entity->isModified()) { $entity->id; } $this->beginTransaction(); $id = $entity->getValue('id', TRUE); $data = $entity->toArray(IEntity::TO_ARRAY_LOADED_RELATIONSHIP_AS_IS); $storageProperties = $entity->getMetadata()->getStorageProperties(); foreach ($data as $key => $value) { if (!in_array($key, $storageProperties, TRUE) || $value instanceof IRelationshipCollection) { unset($data[$key]); } if ($value instanceof IEntity) { $data[$key] = $value->id; } } unset($data['id']); $data = $this->getStorageReflection()->convertEntityToStorage($data); if (!$entity->isPersisted()) { $this->databaseContext->query('INSERT INTO ' . $this->getTableName() . ' ', $data); if ($id) { return $id; } else { return $this->databaseContext->getInsertId($this->databaseStructure->getPrimaryKeySequence($this->getTableName())); } } else { $primary = []; $id = (array) $id; foreach ($this->getStorageReflection()->getStoragePrimaryKey() as $key) { $primary[$key] = array_shift($id); } $this->databaseContext->query('UPDATE ' . $this->getTableName() . ' SET', $data, 'WHERE ?', $primary); return $entity->id; } }
/** * @param string $tableName * @param array $columns * @return void */ protected function generateTableGeneratedHyperRow($tableName, $columns) { $classFqn = $this->config['classes']['row']['generated']; $classFqn = Helpers::substituteClassWildcard($classFqn, $tableName); $className = Helpers::extractClassName($classFqn); $classNamespace = Helpers::extractNamespace($classFqn); $extendsFqn = $this->config['classes']['row']['base']; $extends = Helpers::formatClassName($extendsFqn, $classNamespace); $class = new ClassType($className); $class->setExtends($extends); // Add property annotations based on columns foreach ($columns as $column => $type) { if (in_array($type, [IStructure::FIELD_DATETIME, IStructure::FIELD_TIME, IStructure::FIELD_DATE, IStructure::FIELD_UNIX_TIMESTAMP])) { $type = '\\Nette\\Utils\\DateTime'; } $class->addComment("@property-read {$type} \${$column}"); } // Generate methods.row.getter foreach ((array) $this->config['methods']['row']['getter'] as $methodTemplate) { // Generate column getters foreach ($columns as $column => $type) { if (in_array($type, [IStructure::FIELD_DATETIME, IStructure::FIELD_TIME, IStructure::FIELD_DATE, IStructure::FIELD_UNIX_TIMESTAMP])) { $type = '\\Nette\\Utils\\DateTime'; } $methodName = Helpers::substituteMethodWildcard($methodTemplate, $column); $returnType = $type; $class->addMethod($methodName)->addBody('return $this->activeRow->?;', [$column])->addComment("@return {$returnType}"); // Add property annotation if (Strings::startsWith($methodName, 'get')) { $property = Strings::firstLower(Strings::substring($methodName, 3)); if ($property != $column) { $class->addComment("@property-read {$type} \${$property}"); } } } } // Generate methods.row.ref foreach ((array) $this->config['methods']['row']['ref'] as $methodTemplate) { // Generate 'ref' methods foreach ($this->structure->getBelongsToReference($tableName) as $referencingColumn => $referencedTable) { if (is_array($this->config['tables']) && !in_array($referencedTable, $this->config['tables'])) { continue; } $result = Helpers::underscoreToCamelWithoutPrefix(Strings::replace($referencingColumn, '~_id$~'), $tableName); $methodName = Helpers::substituteMethodWildcard($methodTemplate, $result); $returnType = $this->getTableClass('row', $referencedTable, $classNamespace); $class->addMethod($methodName)->addBody('return $this->ref(?, ?);', [$referencedTable, $referencingColumn])->addComment("@return {$returnType}"); // Add property annotations if (Strings::startsWith($methodName, 'get')) { $property = Strings::firstLower(Strings::substring($methodName, 3)); $class->addComment("@property-read {$returnType} \${$property}"); } } } // Generate methods.row.related foreach ((array) $this->config['methods']['row']['related'] as $methodTemplate) { // Generate 'related' methods foreach ($this->structure->getHasManyReference($tableName) as $relatedTable => $referencingColumns) { if (is_array($this->config['tables']) && !in_array($relatedTable, $this->config['tables'])) { continue; } foreach ($referencingColumns as $referencingColumn) { // Omit longest common prefix between $relatedTable and (this) $tableName $result = Helpers::underscoreToCamelWithoutPrefix($relatedTable, $tableName); if (count($referencingColumns) > 1) { $suffix = 'As' . Helpers::underscoreToCamel(Strings::replace($referencingColumn, '~_id$~')); } else { $suffix = NULL; } $methodName = Helpers::substituteMethodWildcard($methodTemplate, $result, $suffix); $returnType = $this->getTableClass('selection', $relatedTable, $classNamespace); $class->addMethod($methodName)->addBody('return $this->related(?, ?);', [$relatedTable, $referencingColumn])->addComment("@return {$returnType}"); // Add property annotations if (Strings::startsWith($methodName, 'get')) { $property = Strings::firstLower(Strings::substring($methodName, 3)); $class->addComment("@property-read {$returnType} \${$property}"); } } } } $code = implode("\n\n", ['<?php', "/**\n * This is a generated file. DO NOT EDIT. It will be overwritten.\n */", "namespace {$classNamespace};", $class]); $dir = $this->config['dir'] . '/' . 'tables' . '/' . $tableName; $file = $dir . '/' . $className . '.php'; $this->writeIfChanged($file, $code); }