protected function addFieldsFromClass(Doctrine\ORM\Mapping\ClassMetadata $class, $alias, $columnAliasMap)
 {
     foreach ($class->getColumnNames() as $columnName) {
         $propertyName = $class->getFieldName($columnName);
         if ($class->isInheritedField($propertyName)) {
             continue;
         }
         $columnAlias = $this->platform->getSQLResultCasing($columnAliasMap[$columnName]);
         if (isset($this->fieldMappings[$columnAlias])) {
             throw new \InvalidArgumentException("The column '{$columnName}' conflicts with another column in the mapper.");
         }
         $this->addFieldResult($alias, $columnAlias, $propertyName, $class->getName());
     }
 }
 function it_massively_persists_pending_versions($versionBuilder, $versionManager, $normalizer, $connection, $entityManager, $tableNameBuilder, ClassMetadata $versionMetadata, ProductInterface $product1, ProductInterface $product2, Version $pendingVersion1, Version $pendingVersion2, \DateTime $date1, \DateTime $date2)
 {
     $products = [$product1, $product2];
     $date1->format(\DateTime::ISO8601)->willReturn('2014-07-16T10:20:36+02:00');
     $date2->format(\DateTime::ISO8601)->willReturn('2014-07-16T10:20:37+02:00');
     $versionManager->getUsername()->willReturn('julia');
     $versionManager->getContext()->willReturn('CSV Import');
     $normalizedProduct1 = ['sku' => 'sku-001', 'name' => 'my product 1'];
     $normalizedProduct2 = ['sku' => 'sku-002', 'name' => 'my product 2'];
     $normalizer->normalize($product1, 'csv', ['versioning' => true])->willReturn($normalizedProduct1);
     $normalizer->normalize($product2, 'csv', ['versioning' => true])->willReturn($normalizedProduct2);
     $tableNameBuilder->getTableName('VersionClass')->willReturn('version_table');
     $versionMetadata->getColumnNames()->willReturn(['id', 'author', 'changeset', 'snapshot', 'resource_name', 'resource_id', 'context', 'logged_at', 'pending']);
     $versionMetadata->getFieldName('author')->willReturn('author');
     $versionMetadata->getFieldName('changeset')->willReturn('changeset');
     $versionMetadata->getFieldName('snapshot')->willReturn('snapshot');
     $versionMetadata->getFieldName('resource_name')->willReturn('resourceName');
     $versionMetadata->getFieldName('resource_id')->willReturn('resourceId');
     $versionMetadata->getFieldName('context')->willReturn('context');
     $versionMetadata->getFieldName('logged_at')->willReturn('loggedAt');
     $versionMetadata->getFieldName('pending')->willReturn('pending');
     $versionMetadata->getIdentifierColumnNames()->willReturn(['id']);
     $versionMetadata->getFieldValue($pendingVersion1, 'author')->willReturn('julia');
     $versionMetadata->getFieldValue($pendingVersion1, 'context')->willReturn('CSV Import');
     $versionMetadata->getFieldValue($pendingVersion1, 'changeset')->willReturn(serialize($normalizedProduct1));
     $versionMetadata->getFieldValue($pendingVersion1, 'snapshot')->willReturn(null);
     $versionMetadata->getFieldValue($pendingVersion1, 'resourceName')->willReturn('ProductClass');
     $versionMetadata->getFieldValue($pendingVersion1, 'resourceId')->willReturn('myprod1');
     $versionMetadata->getFieldValue($pendingVersion1, 'loggedAt')->willReturn($date1);
     $versionMetadata->getFieldValue($pendingVersion1, 'pending')->willReturn(true);
     $versionMetadata->getFieldValue($pendingVersion2, 'author')->willReturn('julia');
     $versionMetadata->getFieldValue($pendingVersion2, 'context')->willReturn('CSV Import');
     $versionMetadata->getFieldValue($pendingVersion2, 'changeset')->willReturn(serialize($normalizedProduct2));
     $versionMetadata->getFieldValue($pendingVersion2, 'snapshot')->willReturn(null);
     $versionMetadata->getFieldValue($pendingVersion2, 'resourceName')->willReturn('ProductClass');
     $versionMetadata->getFieldValue($pendingVersion2, 'resourceId')->willReturn('myprod2');
     $versionMetadata->getFieldValue($pendingVersion2, 'loggedAt')->willReturn($date2);
     $versionMetadata->getFieldValue($pendingVersion2, 'pending')->willReturn(true);
     $entityManager->getClassMetadata('VersionClass')->willReturn($versionMetadata);
     $versionBuilder->createPendingVersion($product1, 'julia', $normalizedProduct1, 'CSV Import')->willReturn($pendingVersion1);
     $versionBuilder->createPendingVersion($product2, 'julia', $normalizedProduct2, 'CSV Import')->willReturn($pendingVersion2);
     $connection->executeQuery('INSERT INTO version_table' . ' (author,changeset,snapshot,resource_name,resource_id,context,logged_at,pending)' . ' VALUES (?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?)', ['julia', 'a:2:{s:3:"sku";s:7:"sku-001";s:4:"name";s:12:"my product 1";}', null, 'ProductClass', 'myprod1', 'CSV Import', '2014-07-16 08:20:36', true, 'julia', 'a:2:{s:3:"sku";s:7:"sku-002";s:4:"name";s:12:"my product 2";}', null, 'ProductClass', 'myprod2', 'CSV Import', '2014-07-16 08:20:37', true])->shouldBeCalled();
     $this->persistPendingVersions($products);
 }
Esempio n. 3
0
 /**
  * Get the class metadata that is responsible for the definition of the referenced column name.
  *
  * Previously this was a simple task, but with DDC-117 this problem is actually recursive. If its
  * not a simple field, go through all identifier field names that are associations recursivly and
  * find that referenced column name.
  *
  * TODO: Is there any way to make this code more pleasing?
  *
  * @param ClassMetadata $class
  * @param string $referencedColumnName
  * @return array(ClassMetadata, referencedFieldName)
  */
 private function getDefiningClass($class, $referencedColumnName)
 {
     $referencedFieldName = $class->getFieldName($referencedColumnName);
     if ($class->hasField($referencedFieldName)) {
         return array($class, $referencedFieldName);
     } else {
         if (in_array($referencedColumnName, $class->getIdentifierColumnNames())) {
             // it seems to be an entity as foreign key
             foreach ($class->getIdentifierFieldNames() as $fieldName) {
                 if ($class->hasAssociation($fieldName) && $class->getSingleAssociationJoinColumnName($fieldName) == $referencedColumnName) {
                     return $this->getDefiningClass($this->em->getClassMetadata($class->associationMappings[$fieldName]['targetEntity']), $class->getSingleAssociationReferencedJoinColumnName($fieldName));
                 }
             }
         }
     }
     return null;
 }
Esempio n. 4
0
 /**
  * Gather columns and fk constraints that are required for one part of relationship.
  *
  * @param array $joinColumns
  * @param \Doctrine\DBAL\Schema\Table $theJoinTable
  * @param ClassMetadata $class
  * @param \Doctrine\ORM\Mapping\AssociationMapping $mapping
  * @param array $primaryKeyColumns
  * @param array $uniqueConstraints
  */
 private function _gatherRelationJoinColumns($joinColumns, $theJoinTable, $class, $mapping, &$primaryKeyColumns, &$uniqueConstraints)
 {
     $localColumns = array();
     $foreignColumns = array();
     $fkOptions = array();
     foreach ($joinColumns as $joinColumn) {
         $columnName = $joinColumn['name'];
         $referencedFieldName = $class->getFieldName($joinColumn['referencedColumnName']);
         if (!$class->hasField($referencedFieldName)) {
             throw new \Doctrine\ORM\ORMException("Column name `" . $joinColumn['referencedColumnName'] . "` referenced for relation from " . $mapping['sourceEntity'] . " towards " . $mapping['targetEntity'] . " does not exist.");
         }
         $primaryKeyColumns[] = $columnName;
         $localColumns[] = $columnName;
         $foreignColumns[] = $joinColumn['referencedColumnName'];
         if (!$theJoinTable->hasColumn($joinColumn['name'])) {
             // Only add the column to the table if it does not exist already.
             // It might exist already if the foreign key is mapped into a regular
             // property as well.
             $fieldMapping = $class->getFieldMapping($referencedFieldName);
             $columnDef = null;
             if (isset($joinColumn['columnDefinition'])) {
                 $columnDef = $joinColumn['columnDefinition'];
             } else {
                 if (isset($fieldMapping['columnDefinition'])) {
                     $columnDef = $fieldMapping['columnDefinition'];
                 }
             }
             $columnOptions = array('notnull' => false, 'columnDefinition' => $columnDef);
             if (isset($joinColumn['nullable'])) {
                 $columnOptions['notnull'] = !$joinColumn['nullable'];
             }
             if ($fieldMapping['type'] == "string") {
                 $columnOptions['length'] = $fieldMapping['length'];
             } else {
                 if ($fieldMapping['type'] == "decimal") {
                     $columnOptions['scale'] = $fieldMapping['scale'];
                     $columnOptions['precision'] = $fieldMapping['precision'];
                 }
             }
             $theJoinTable->addColumn($columnName, $class->getTypeOfColumn($joinColumn['referencedColumnName']), $columnOptions);
         }
         if (isset($joinColumn['unique']) && $joinColumn['unique'] == true) {
             $uniqueConstraints[] = array('columns' => array($columnName));
         }
         if (isset($joinColumn['onUpdate'])) {
             $fkOptions['onUpdate'] = $joinColumn['onUpdate'];
         }
         if (isset($joinColumn['onDelete'])) {
             $fkOptions['onDelete'] = $joinColumn['onDelete'];
         }
     }
     $theJoinTable->addUnnamedForeignKeyConstraint($class->getTableName(), $localColumns, $foreignColumns, $fkOptions);
 }