Beispiel #1
0
 /**
  * Returns the type-casted and stringified default value for the specified
  * Column. This only works for scalar default values currently.
  *
  * @param  Column          $column
  * @throws EngineException
  * @return string
  */
 protected function getDefaultValueString(Column $column)
 {
     $defaultValue = var_export(null, true);
     $val = $column->getPhpDefaultValue();
     if (null === $val) {
         return $defaultValue;
     }
     if ($column->isTemporalType()) {
         $fmt = $this->getTemporalFormatter($column);
         try {
             if (!($this->getPlatform() instanceof MysqlPlatform && ($val === '0000-00-00 00:00:00' || $val === '0000-00-00'))) {
                 // while technically this is not a default value of NULL,
                 // this seems to be closest in meaning.
                 $defDt = new \DateTime($val);
                 $defaultValue = var_export($defDt->format($fmt), true);
             }
         } catch (\Exception $exception) {
             // prevent endless loop when timezone is undefined
             date_default_timezone_set('America/Los_Angeles');
             throw new EngineException(sprintf('Unable to parse default temporal value "%s" for column "%s"', $column->getDefaultValueString(), $column->getFullyQualifiedName()), 0, $exception);
         }
     } elseif ($column->isEnumType()) {
         $valueSet = $column->getValueSet();
         if (!in_array($val, $valueSet)) {
             throw new EngineException(sprintf('Default Value "%s" is not among the enumerated values', $val));
         }
         $defaultValue = array_search($val, $valueSet);
     } elseif ($column->isSetType()) {
         $defaultValue = SetColumnConverter::convertToInt($val, $column->getValueSet());
     } elseif ($column->isPhpPrimitiveType()) {
         settype($val, $column->getPhpType());
         $defaultValue = var_export($val, true);
     } elseif ($column->isPhpObjectType()) {
         $defaultValue = 'new ' . $column->getPhpType() . '(' . var_export($val, true) . ')';
     } elseif ($column->isPhpArrayType()) {
         $defaultValue = var_export($val, true);
     } else {
         throw new EngineException("Cannot get default value string for " . $column->getFullyQualifiedName());
     }
     return $defaultValue;
 }
Beispiel #2
0
 /**
  * Converts value for some column types
  *
  * @param  mixed     $value  The value to convert
  * @param  ColumnMap $colMap The ColumnMap object
  * @return mixed     The converted value
  */
 protected function convertValueForColumn($value, ColumnMap $colMap)
 {
     if ($colMap->getType() == 'OBJECT' && is_object($value)) {
         if (is_array($value)) {
             $value = array_map('serialize', $value);
         } else {
             $value = serialize($value);
         }
     } elseif ('ARRAY' === $colMap->getType() && is_array($value)) {
         $value = '| ' . implode(' | ', $value) . ' |';
     } elseif (PropelTypes::ENUM === $colMap->getType() && !is_null($value)) {
         if (is_array($value)) {
             $value = array_map([$colMap, 'getValueSetKey'], $value);
         } else {
             $value = $colMap->getValueSetKey($value);
         }
     } elseif ($colMap->isSetType() && !is_null($value)) {
         try {
             $value = SetColumnConverter::convertToInt($value, $colMap->getValueSet());
         } catch (SetColumnConverterException $e) {
             throw new PropelException(sprintf('Value "%s" is not accepted in this set column', $e->getValue()), $e->getCode(), $e);
         }
     }
     return $value;
 }
Beispiel #3
0
 /**
  * Returns the SQL for the default value of a Column object
  * @return string
  */
 public function getColumnDefaultValueDDL(Column $col)
 {
     $default = '';
     $defaultValue = $col->getDefaultValue();
     if (null !== $defaultValue) {
         $default .= 'DEFAULT ';
         if ($defaultValue->isExpression()) {
             $default .= $defaultValue->getValue();
         } else {
             if ($col->isTextType()) {
                 $default .= $this->quote($defaultValue->getValue());
             } elseif (in_array($col->getType(), [PropelTypes::BOOLEAN, PropelTypes::BOOLEAN_EMU])) {
                 $default .= $this->getBooleanString($defaultValue->getValue());
             } elseif ($col->getType() == PropelTypes::ENUM) {
                 $default .= array_search($defaultValue->getValue(), $col->getValueSet());
             } elseif ($col->isSetType()) {
                 $val = trim($defaultValue->getValue());
                 $values = [];
                 foreach (explode(',', $val) as $v) {
                     $values[] = trim($v);
                 }
                 $default .= SetColumnConverter::convertToInt($values, $col->getValueSet());
             } elseif ($col->isPhpArrayType()) {
                 $value = $this->getPhpArrayString($defaultValue->getValue());
                 if (null === $value) {
                     $default = '';
                 } else {
                     $default .= $value;
                 }
             } else {
                 $default .= $defaultValue->getValue();
             }
         }
     }
     return $default;
 }
 /**
  * @expectedException \Propel\Common\Exception\SetColumnConverterException
  */
 public function testConvertIntToArrayIntOutOfRange()
 {
     $valueSet = ['a', 'b', 'c', 'd', 'e', 'f'];
     SetColumnConverter::convertIntToArray('65', $valueSet);
 }