Example #1
0
 public function getOrCreateTableStructure($table, $primaryKey)
 {
     if (!isset($this->tableStructures[$table])) {
         $tableColumns = $this->getSource()->getTableColumns($table, true);
         $tableStructure = new TableStructure($table, $primaryKey);
         Mesour\Sources\Helpers::setStructureFromColumns($tableStructure, $tableColumns);
         $this->tableStructures[$table] = $tableStructure;
     }
     return $this->getTableStructure($table);
 }
Example #2
0
 protected function addPatternToRows(BaseTableColumnStructure $columnStructure, &$items)
 {
     if (is_array($items) || $items instanceof ArrayHash) {
         foreach ($items as $key => $item) {
             if (is_array($item) || $item instanceof ArrayHash) {
                 if ($columnStructure->getPattern()) {
                     $item['_pattern'] = Helpers::parseValue($columnStructure->getPattern(), $item);
                 } else {
                     $item['_pattern'] = null;
                 }
             }
             $items[$key] = $item;
         }
     } else {
         $items = [];
     }
 }
Example #3
0
 protected function initializeDataStructure($tableName, $primaryKey)
 {
     $dataStructure = new Structures\DataStructure($tableName, $primaryKey);
     $this->setDataStructure($dataStructure);
     $structure = $this->context->getStructure();
     $columns = $structure->getColumns($this->getTableName());
     Helpers::setStructureFromColumns($dataStructure, $this->determineFromColumns($columns));
     foreach ($structure->getBelongsToReference($tableName) as $key => $table) {
         $dataStructure->getOrCreateTableStructure($table, $structure->getPrimaryKey($table));
         $targetReference = $structure->getBelongsToReference($table);
         $hasOneToOne = array_search($tableName, $targetReference);
         if ($hasOneToOne) {
             $field = $dataStructure->addOneToOne($table, $table, $key);
         } else {
             $field = $dataStructure->addManyToOne($table, $table, $key);
         }
         $column = $this->findColumn($columns, $key);
         $field->setNullable($column['nullable']);
     }
     foreach ($structure->getHasManyReference($tableName) as $table => $keys) {
         $dataStructure->getOrCreateTableStructure($table, $structure->getPrimaryKey($table));
         $sourceReference = $structure->getBelongsToReference($tableName);
         if (in_array($table, $sourceReference)) {
             continue;
         }
         $targetReference = $structure->getBelongsToReference($table);
         if (count($targetReference) > 1) {
             $match = null;
             foreach ($keys as $key => $targetTable) {
                 $match = array_search($tableName, $targetReference);
                 if ($match) {
                     unset($targetReference[$match]);
                     break;
                 }
             }
             if ($match) {
                 $arrayKeys = array_keys($targetReference);
                 $selfColumn = reset($arrayKeys);
                 $currentValue = reset($targetReference);
                 $dataStructure->getOrCreateTableStructure($currentValue, $structure->getPrimaryKey($currentValue));
                 $dataStructure->addManyToMany($currentValue, $currentValue, $selfColumn, $table, $match);
                 continue;
             }
         }
         $match = null;
         foreach ($keys as $key => $targetTable) {
             $match = array_search($tableName, $targetReference);
             if ($match) {
                 unset($targetReference[$match]);
                 break;
             }
         }
         if ($match) {
             $dataStructure->addOneToMany($table, $table, $match);
         }
     }
 }
Example #4
0
 protected function initializeDataStructure($tableName, $primaryKey)
 {
     $dataStructure = new Structures\DataStructure($tableName, $primaryKey);
     $this->setDataStructure($dataStructure);
     $entityManager = $this->queryBuilder->getEntityManager();
     $classMetadata = $entityManager->getClassMetadata($tableName);
     Helpers::setStructureFromColumns($dataStructure, $this->determineFromColumns($classMetadata));
     foreach ($classMetadata->getAssociationNames() as $associationName) {
         $targetClass = $classMetadata->getAssociationTargetClass($associationName);
         $targetMetadata = $entityManager->getClassMetadata($targetClass);
         $associations = $classMetadata->getAssociationsByTargetClass($targetClass);
         $primaryColumns = $targetMetadata->getIdentifierColumnNames();
         foreach ($associations as $association) {
             $dataStructure->getOrCreateTableStructure($targetClass, reset($primaryColumns));
             $associationsType = $association['type'];
             if ($associationsType === $classMetadata::ONE_TO_ONE) {
                 if ($association['sourceEntity'] === $tableName) {
                     $referencedColumn = reset($association['targetToSourceKeyColumns']);
                 }
                 if (!isset($referencedColumn)) {
                     throw new InvalidStateException(sprintf('Can not find referenced column name for field %s.', $associationName));
                 }
                 $dataStructure->addOneToOne($associationName, $targetClass, $referencedColumn);
             } elseif ($associationsType === $classMetadata::MANY_TO_ONE) {
                 if ($association['sourceEntity'] === $tableName) {
                     $referencedColumn = reset($association['targetToSourceKeyColumns']);
                 }
                 if (!isset($referencedColumn)) {
                     throw new InvalidStateException(sprintf('Can not find referenced column name for field %s.', $associationName));
                 }
                 $dataStructure->addManyToOne($associationName, $targetClass, $referencedColumn);
             } elseif ($associationsType === $classMetadata::ONE_TO_MANY) {
                 foreach ($targetMetadata->getAssociationMappings() as $joinedAssociation) {
                     if ($joinedAssociation['inversedBy'] === $associationName) {
                         $referencedColumn = reset($joinedAssociation['targetToSourceKeyColumns']);
                     }
                 }
                 if (!isset($referencedColumn)) {
                     throw new InvalidStateException(sprintf('Can not find referenced column name for field %s. Is `inversedBy` set in property annotation?', $associationName));
                 }
                 $dataStructure->addOneToMany($associationName, $targetClass, $referencedColumn);
             } elseif ($associationsType === $classMetadata::MANY_TO_MANY) {
                 if (!isset($association['joinTable'])) {
                     throw new InvalidStateException(sprintf('Can not find joinColumn settings for field %s. Is `joinColumn` set in property annotation?', $associationName));
                 }
                 $joinTable = $association['joinTable'];
                 if (isset($joinTable['joinColumns']) && $joinTable['joinColumns']) {
                     $joinColumn = reset($joinTable['joinColumns']);
                     $inverseJoinColumn = reset($joinTable['inverseJoinColumns']);
                     $dataStructure->addManyToMany($associationName, $targetClass, $inverseJoinColumn['name'], $joinTable['name'], $joinColumn['name']);
                 }
             }
         }
     }
 }