/** * Extract the foreign relation field * @param ColumnMap $mappedColumn * @param $field * @return Field */ public static function extractForeignModelsField(ColumnMap $mappedColumn, $field) { $fDto = new Field($field, _($field)); $fDto->type = Field::COMBO_TYPE; $fDto->required = $mappedColumn->isNotNull(); $relatedModel = strtolower($mappedColumn->getRelation()->getForeignTable()->getPhpName()); $fDto->entity = $relatedModel; $fDto->url = Router::getInstance()->getRoute('api-' . $relatedModel . '-pk'); return $fDto; }
/** * Create a new instance. * * @param Criteria $parent The outer class (this is an "inner" class). * @param ColumnMap $column A Column object to help escaping the value * @param mixed $value * @param string $comparison, among ModelCriteria::MODEL_CLAUSE * @param string $clause A simple pseudo-SQL clause, e.g. 'foo.BAR LIKE ?' */ public function __construct(Criteria $outer, $column, $value = null, $comparison = ModelCriteria::MODEL_CLAUSE, $clause, $type = null) { $this->value = $value; if ($column instanceof ColumnMap) { $this->column = $column->getName(); $this->table = $column->getTable()->getName(); } else { $dotPos = strrpos($column, '.'); if ($dotPos === false) { // no dot => aliased column $this->table = null; $this->column = $column; } else { $this->table = substr($column, 0, $dotPos); $this->column = substr($column, $dotPos + 1, strlen($column)); } } $this->comparison = $comparison === null ? Criteria::EQUAL : $comparison; $this->clause = $clause; $this->type = $type; $this->init($outer); }
public function testPrimaryStringAddConfiguredColumn() { $this->assertFalse($this->tmap->hasPrimaryStringColumn(), 'hasPrimaryStringColumn() returns false while none set.'); $column = new ColumnMap('BAR', $this->tmap); $column->setPhpName('Bar'); $column->setType('VARCHAR'); $column->setPrimaryString(true); $this->tmap->addConfiguredColumn($column); $this->assertTrue($this->tmap->hasPrimaryStringColumn(), 'hasPrimaryStringColumn() returns true after adding pkStr column.'); $this->assertEquals($column, $this->tmap->getPrimaryStringColumn(), 'getPrimaryStringColumn() returns correct column.'); }
/** * Get a ColumnMap for the table. * * @param string $name A String with the name of the table. * @param boolean $normalize Normalize the column name (if column name not like FIRST_NAME) * @return \Propel\Runtime\Map\ColumnMap A ColumnMap. * @throws \Propel\Runtime\Map\Exception\ColumnNotFoundException If the column is undefined */ public function getColumn($name, $normalize = true) { if ($normalize) { $name = ColumnMap::normalizeName($name); } if (!$this->hasColumn($name, false)) { throw new ColumnNotFoundException(sprintf('Cannot fetch ColumnMap for undefined column: %s.', $name)); } return $this->columns[$name]; }
/** * 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 ('ENUM' === $colMap->getType()) { if (is_array($value)) { $value = array_map(array($colMap, 'getValueSetKey'), $value); } else { $value = $colMap->getValueSetKey($value); } } return $value; }
/** * Binds a value to a positioned parameter in a statement, * given a ColumnMap object to infer the binding type. * * @param StatementInterface $stmt The statement to bind * @param string $parameter Parameter identifier * @param mixed $value The value to bind * @param ColumnMap $cMap The ColumnMap of the column to bind * @param null|integer $position The position of the parameter to bind * * @return boolean */ public function bindValue(StatementInterface $stmt, $parameter, $value, ColumnMap $cMap, $position = null) { if ($cMap->isTemporal()) { $value = $this->formatTemporalValue($value, $cMap); } elseif (is_resource($value) && $cMap->isLob()) { // we always need to make sure that the stream is rewound, otherwise nothing will // get written to database. rewind($value); } return $stmt->bindValue($parameter, $value, $cMap->getPdoType()); }
/** * Normalizes the column name, removing table prefix and uppercasing. * article.first_name becomes FIRST_NAME * * @deprecated Use ColumnMap::normalizeColumName() instead * @param string $name * @return string Normalized column name. */ protected function normalizeColName($name) { return ColumnMap::normalizeName($name); }
public function testNormalizeName() { $this->assertEquals('', ColumnMap::normalizeName(''), 'normalizeColumnName() returns an empty string when passed an empty string'); $this->assertEquals('BAR', ColumnMap::normalizeName('bar'), 'normalizeColumnName() uppercases the input'); $this->assertEquals('BAR_BAZ', ColumnMap::normalizeName('bar_baz'), 'normalizeColumnName() does not mind underscores'); $this->assertEquals('BAR', ColumnMap::normalizeName('FOO.BAR'), 'normalizeColumnName() removes table prefix'); $this->assertEquals('BAR', ColumnMap::normalizeName('BAR'), 'normalizeColumnName() leaves normalized column names unchanged'); $this->assertEquals('BAR_BAZ', ColumnMap::normalizeName('foo.bar_baz'), 'normalizeColumnName() can do all the above at the same time'); }
/** * @see DBAdapter::bindValue() * * @param PDOStatement $stmt * @param string $parameter * @param mixed $value * @param ColumnMap $cMap * @param null|integer $position * * @return boolean */ public function bindValue(PDOStatement $stmt, $parameter, $value, ColumnMap $cMap, $position = null) { if ($cMap->isTemporal()) { $value = $this->formatTemporalValue($value, $cMap); } elseif ($cMap->getType() == PropelColumnTypes::CLOB_EMU) { return $stmt->bindParam(':p' . $position, $value, $cMap->getPdoType(), strlen($value)); } elseif (is_resource($value) && $cMap->isLob()) { // we always need to make sure that the stream is rewound, otherwise nothing will // get written to database. rewind($value); } return $stmt->bindValue($parameter, $value, $cMap->getPdoType()); }
/** * @see AdapterInterface::bindValue() * * @param StatementInterface $stmt * @param string $parameter * @param mixed $value * @param ColumnMap $cMap * @param null|integer $position * * @return boolean */ public function bindValue(StatementInterface $stmt, $parameter, $value, ColumnMap $cMap, $position = null) { $pdoType = $cMap->getPdoType(); // FIXME - This is a temporary hack to get around apparent bugs w/ PDO+MYSQL // See http://pecl.php.net/bugs/bug.php?id=9919 if (\PDO::PARAM_BOOL === $pdoType) { $value = (int) $value; $pdoType = \PDO::PARAM_INT; return $stmt->bindValue($parameter, $value, $pdoType); } if ($cMap->isTemporal()) { $value = $this->formatTemporalValue($value, $cMap); } elseif (is_resource($value) && $cMap->isLob()) { // we always need to make sure that the stream is rewound, otherwise nothing will // get written to database. rewind($value); } return $stmt->bindValue($parameter, $value, $pdoType); }
/** * @see AdapterInterface::bindValue() * * @param PDOStatement $stmt * @param string $parameter * @param mixed $value * @param ColumnMap $cMap * @param null|integer $position * * @return boolean */ public function bindValue(StatementInterface $stmt, $parameter, $value, ColumnMap $cMap, $position = null) { if ($cMap->isTemporal()) { $value = $this->formatTemporalValue($value, $cMap); } elseif (is_resource($value) && $cMap->isLob()) { // we always need to make sure that the stream is rewound, otherwise nothing will // get written to database. rewind($value); // pdo_sqlsrv must have bind binaries using bindParam so that the PDO::SQLSRV_ENCODING_BINARY // driver option can be utilized. This requires a unique blob parameter because the bindParam // value is passed by reference and if we didn't do this then the referenced parameter value // would change on the next loop $blob = 'blob' . $position; ${$blob} = $value; return $stmt->bindParam($parameter, ${$blob}, \PDO::PARAM_LOB, 0, \PDO::SQLSRV_ENCODING_BINARY); } return $stmt->bindValue($parameter, $value, $cMap->getPdoType()); }
public function getPrimaryKeys() { $cm = new ColumnMap('id', new TableMap()); $cm->setType('INTEGER'); return array('id' => $cm); }
/** * 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; }
/** * Whether this column contains scalar values (to be used as indices). * * @param ColumnMap $column * * @return bool */ private function isScalar(ColumnMap $column) { return in_array($column->getPdoType(), array(\PDO::PARAM_BOOL, \PDO::PARAM_INT, \PDO::PARAM_STR)); }
/** * @param ColumnMap $columnMap * @return bool */ protected function isColumnBehavior(ColumnMap $columnMap) { foreach ($columnMap->getTable()->getBehaviors() as $name => $params) { $columnName = Base::toLower($columnMap->getName()); switch ($name) { case 'nested_set': $columnNames = array($params['left_column'], $params['right_column'], $params['level_column']); if (in_array($columnName, $columnNames)) { return true; } break; case 'timestampable': $columnNames = array($params['create_column'], $params['update_column']); if (in_array($columnName, $columnNames)) { return true; } break; } } return false; }
/** * @param ColumnMap $column * @return \Closure|null */ public function guessFormat(ColumnMap $column) { $generator = $this->generator; if ($column->isTemporal()) { if ($column->getType() == PropelTypes::BU_DATE || $column->getType() == PropelTypes::BU_TIMESTAMP) { return function () use($generator) { return $generator->dateTime; }; } else { return function () use($generator) { return $generator->dateTimeAD; }; } } $type = $column->getType(); switch ($type) { case PropelTypes::BOOLEAN: case PropelTypes::BOOLEAN_EMU: return function () use($generator) { return $generator->boolean; }; case PropelTypes::NUMERIC: case PropelTypes::DECIMAL: $size = $column->getSize(); return function () use($generator, $size) { return $generator->randomNumber($size + 2) / 100; }; case PropelTypes::TINYINT: return function () { return mt_rand(0, 127); }; case PropelTypes::SMALLINT: return function () { return mt_rand(0, 32767); }; case PropelTypes::INTEGER: return function () { return mt_rand(0, intval('2147483647')); }; case PropelTypes::BIGINT: return function () { return mt_rand(0, intval('9223372036854775807')); }; case PropelTypes::FLOAT: return function () { return mt_rand(0, intval('2147483647')) / mt_rand(1, intval('2147483647')); }; case PropelTypes::DOUBLE: case PropelTypes::REAL: return function () { return mt_rand(0, intval('9223372036854775807')) / mt_rand(1, intval('9223372036854775807')); }; case PropelTypes::CHAR: case PropelTypes::VARCHAR: case PropelTypes::BINARY: case PropelTypes::VARBINARY: $size = $column->getSize(); return function () use($generator, $size) { return $generator->text($size); }; case PropelTypes::LONGVARCHAR: case PropelTypes::LONGVARBINARY: case PropelTypes::CLOB: case PropelTypes::CLOB_EMU: case PropelTypes::BLOB: return function () use($generator) { return $generator->text; }; case PropelTypes::ENUM: $valueSet = $column->getValueSet(); return function () use($generator, $valueSet) { return $generator->randomElement($valueSet); }; case PropelTypes::OBJECT: case PropelTypes::PHP_ARRAY: default: // no smart way to guess what the user expects here return null; } }