Ejemplo n.º 1
0
Archivo: DBAL.php Proyecto: lynx/lynx
 /**
  * @param $entity
  * @return array
  */
 protected function prepareSet($entity)
 {
     $data = [];
     foreach ($this->metaData->getFieldNames() as $fieldName) {
         $columnName = $this->metaData->getColumnName($fieldName);
         $type = $this->metaData->getTypeOfField($fieldName);
         $fieldValue = $entity->{$fieldName};
         $type = \Doctrine\DBAL\Types\Type::getType($type);
         $value = $type->convertToDatabaseValue($fieldValue, $this->em->getConnection()->getDatabasePlatform());
         $data[$columnName] = $value;
     }
     return $data;
 }
 /**
  * Retrieves the default version value which was created
  * by the preceding INSERT statement and assigns it back in to the 
  * entities version field.
  *
  * @param Doctrine\ORM\Mapping\ClassMetadata $class
  * @param object $entity
  * @param mixed $id
  */
 protected function _assignDefaultVersionValue($class, $entity, $id)
 {
     $versionField = $this->_class->versionField;
     $identifier = $this->_class->getIdentifierColumnNames();
     $versionFieldColumnName = $this->_class->getColumnName($versionField);
     //FIXME: Order with composite keys might not be correct
     $sql = "SELECT " . $versionFieldColumnName . " FROM " . $class->getQuotedTableName($this->_platform) . " WHERE " . implode(' = ? AND ', $identifier) . " = ?";
     $value = $this->_conn->fetchColumn($sql, array_values((array) $id));
     $this->_class->setFieldValue($entity, $versionField, $value);
 }
 /**
  * Prepares the data changeset of an entity for database insertion.
  *
  * @param object $entity
  * @param boolean $isInsert Whether the preparation is for an INSERT (or UPDATE, if FALSE).
  * 
  * return The reference to the data array.
  * @access private
  *
  * @author Etienne de Longeaux <*****@*****.**>
  */
 private function _prepareData($entity, $isInsert = false)
 {
     $result = array();
     $platform = $this->_conn->getDatabasePlatform();
     $uow = $this->_em->getUnitOfWork();
     foreach ($this->_reflFields as $field => $ReflectionProperty) {
         $newVal = $this->_class->getFieldValue($entity, $field);
         $columnName = $this->_class->getColumnName($field);
         if (isset($this->_class->associationMappings[$field])) {
             $assocMapping = $this->_class->associationMappings[$field];
             // Only owning side of x-1 associations can have a FK column.
             if (!$assocMapping['isOwningSide']) {
                 continue;
             }
             // Special case: One-one self-referencing of the same class with IDENTITY type key generation.
             if ($this->_class->isIdGeneratorIdentity() && $newVal !== null && $assocMapping['sourceEntity'] == $assocMapping['targetEntity']) {
                 $oid = spl_object_hash($newVal);
                 $isScheduledForInsert = $uow->isScheduledForInsert($newVal);
                 if (isset($this->_queuedInserts[$oid]) || $isScheduledForInsert) {
                     // The associated entity $newVal is not yet persisted, so we must
                     // set $newVal = null, in order to insert a null value and schedule an
                     // extra update on the UnitOfWork.
                     $uow->scheduleExtraUpdate($entity, array($field => array(null, $newVal)));
                     $newVal = null;
                 } else {
                     if ($isInsert && !$isScheduledForInsert && $uow->getEntityState($newVal) == UnitOfWork::STATE_MANAGED) {
                         // $newVal is already fully persisted.
                         // Schedule an extra update for it, so that the foreign key(s) are properly set.
                         $uow->scheduleExtraUpdate($newVal, array($field => array(null, $entity)));
                     }
                 }
             }
             foreach ($assocMapping['sourceToTargetKeyColumns'] as $sourceColumn => $targetColumn) {
                 if ($newVal === null) {
                     $result[$sourceColumn] = null;
                 } else {
                     $otherClass = $this->_em->getClassMetadata($assocMapping['targetEntity']);
                     $result[$sourceColumn] = $otherClass->reflFields[$otherClass->fieldNames[$targetColumn]]->getValue($newVal);
                 }
             }
         } else {
             if ($newVal === null) {
                 $result[$columnName] = null;
             } else {
                 $result[$columnName] = Type::getType($this->_class->fieldMappings[$field]['type'])->convertToDatabaseValue($newVal, $platform);
             }
         }
     }
     return $result;
 }
Ejemplo n.º 4
0
 /**
  * Prepares the data changeset of an entity for database insertion.
  * The array that is passed as the second parameter is filled with
  * <columnName> => <value> pairs, grouped by table name, during this preparation.
  * 
  * Example:
  * <code>
  * array(
  *    'foo_table' => array('column1' => 'value1', 'column2' => 'value2', ...),
  *    'bar_table' => array('columnX' => 'valueX', 'columnY' => 'valueY', ...),
  *    ...
  * )
  * </code>
  * 
  * Notes to inheritors: Be sure to call <code>parent::_prepareData($entity, $result, $isInsert);</code>
  *
  * @param object $entity
  * @param array $result The reference to the data array.
  * @param boolean $isInsert
  */
 protected function _prepareData($entity, array &$result, $isInsert = false)
 {
     $platform = $this->_conn->getDatabasePlatform();
     $uow = $this->_em->getUnitOfWork();
     foreach ($uow->getEntityChangeSet($entity) as $field => $change) {
         $oldVal = $change[0];
         $newVal = $change[1];
         $columnName = $this->_class->getColumnName($field);
         if (isset($this->_class->associationMappings[$field])) {
             $assocMapping = $this->_class->associationMappings[$field];
             // Only owning side of x-1 associations can have a FK column.
             if (!$assocMapping->isOneToOne() || $assocMapping->isInverseSide()) {
                 continue;
             }
             //TODO: If the one-one is self-referencing, check whether the referenced entity ($newVal)
             //      is still scheduled for insertion. If so:
             //      1) set $newVal = null, so that we insert a null value
             //      2) schedule $entity for an update, so that the FK gets set through an update
             //         later, after the referenced entity has been inserted.
             //$needsPostponedUpdate = ...
             /*if ($assocMapping->sourceEntityName == $assocMapping->targetEntityName &&
                       isset($this->_queuedInserts[spl_object_hash($entity)])) {
               	echo "SELF-REFERENCING!";
               }*/
             foreach ($assocMapping->sourceToTargetKeyColumns as $sourceColumn => $targetColumn) {
                 $otherClass = $this->_em->getClassMetadata($assocMapping->targetEntityName);
                 if ($newVal === null) {
                     $result[$this->getOwningTable($field)][$sourceColumn] = null;
                 } else {
                     $result[$this->getOwningTable($field)][$sourceColumn] = $otherClass->reflFields[$otherClass->fieldNames[$targetColumn]]->getValue($newVal);
                 }
             }
         } else {
             if ($newVal === null) {
                 $result[$this->getOwningTable($field)][$columnName] = null;
             } else {
                 $result[$this->getOwningTable($field)][$columnName] = Type::getType($this->_class->fieldMappings[$field]['type'])->convertToDatabaseValue($newVal, $platform);
             }
         }
     }
 }
Ejemplo n.º 5
0
 /**
  * Fetch the current version value of a versioned entity.
  *
  * @param Doctrine\ORM\Mapping\ClassMetadata $versionedClass
  * @param mixed $id
  * @return mixed
  */
 protected function fetchVersionValue($versionedClass, $id)
 {
     $versionField = $versionedClass->versionField;
     $identifier = $versionedClass->getIdentifierColumnNames();
     $versionFieldColumnName = $versionedClass->getColumnName($versionField);
     //FIXME: Order with composite keys might not be correct
     $sql = "SELECT " . $versionFieldColumnName . " FROM " . $versionedClass->getQuotedTableName($this->_platform) . " WHERE " . implode(' = ? AND ', $identifier) . " = ?";
     $value = $this->_conn->fetchColumn($sql, array_values((array) $id));
     return Type::getType($versionedClass->fieldMappings[$versionField]['type'])->convertToPHPValue($value, $this->_platform);
 }
Ejemplo n.º 6
0
 /**
  * Initialize extended field options by field.
  *
  * @param ClassMetadata $classMetadata
  */
 protected function initializeExtendedFieldsOptions(ClassMetadata $classMetadata)
 {
     $configManager = $this->getConfigManager();
     $className = $classMetadata->getName();
     $tableName = $classMetadata->getTableName();
     foreach ($classMetadata->getFieldNames() as $fieldName) {
         if ($configManager->hasConfig($className, $fieldName)) {
             $columnName = $classMetadata->getColumnName($fieldName);
             $options = $this->getExtendedFieldOptions($className, $fieldName);
             if (!empty($options['extend']['is_extend'])) {
                 $this->extendedFieldOptions[$tableName][$columnName] = $options;
             }
         }
     }
 }
Ejemplo n.º 7
0
 private function determineFromColumns(ClassMetadata $classMetadata)
 {
     $out = [];
     /** @var Column $column */
     foreach ($classMetadata->getFieldNames() as $fieldName) {
         $fieldMapping = [];
         try {
             $fieldMapping = $classMetadata->getFieldMapping($fieldName);
         } catch (MappingException $e) {
             $fieldMapping['type'] = 'undefined';
         }
         $columnName = $classMetadata->getColumnName($fieldName);
         $type = $classMetadata->getTypeOfField($fieldName);
         $isNullable = $classMetadata->isNullable($fieldName);
         if (strtolower($type) === 'enum') {
             $out[$columnName] = ['type' => IColumnStructure::ENUM];
             $enum = $fieldMapping['columnDefinition'];
             $options = str_getcsv(str_replace('enum(', '', substr($enum, 0, strlen($enum) - 1)), ',', "'");
             $out[$columnName]['values'] = [];
             foreach ($options as $option) {
                 $out[$columnName]['values'][] = $option;
             }
         } else {
             switch ($type) {
                 case Type::STRING:
                 case Type::TEXT:
                     $out[$columnName] = ['type' => IColumnStructure::TEXT];
                     break;
                 case Type::INTEGER:
                 case Type::FLOAT:
                 case Type::SMALLINT:
                 case Type::BIGINT:
                 case Type::DECIMAL:
                     $out[$columnName] = ['type' => IColumnStructure::NUMBER];
                     break;
                 case Type::DATETIME:
                 case Type::DATETIMETZ:
                 case Type::DATE:
                 case Type::TIME:
                     $out[$columnName] = ['type' => IColumnStructure::DATE];
                     break;
                 case Type::BOOLEAN:
                     $out[$columnName] = ['type' => IColumnStructure::BOOL];
                     break;
             }
         }
         if (isset($out[$columnName])) {
             $out[$columnName]['nullable'] = $isNullable;
         }
     }
     return $out;
 }