protected function addTranslatedColumnSetter(Column $column) { $visibility = $column->getTable()->isReadOnly() ? 'protected' : $column->getMutatorVisibility(); $typeHint = ''; $null = ''; if ($column->getTypeHint()) { $typeHint = $column->getTypeHint(); if ('array' !== $typeHint) { $typeHint = $this->declareClass($typeHint); } $typeHint .= ' '; if (!$column->isNotNull()) { $null = ' = null'; } } $typeHint = "{$typeHint}\$v{$null}"; $i18nTablePhpName = $this->builder->getClassNameFromBuilder($this->builder->getNewStubObjectBuilder($this->behavior->getI18nTable())); $tablePhpName = $this->builder->getObjectClassName(); $objectBuilder = $this->builder->getNewObjectBuilder($this->behavior->getI18nTable()); $comment = ''; if ($this->isDateType($column->getType())) { $objectBuilder->addTemporalMutatorComment($comment, $column); } else { $objectBuilder->addMutatorComment($comment, $column); } $comment = preg_replace('/^\\t/m', '', $comment); $comment = str_replace('@return $this|' . $i18nTablePhpName, '@return $this|' . $tablePhpName, $comment); return $this->renderTemplate('objectTranslatedColumnSetter', ['comment' => $comment, 'column' => $column, 'visibility' => $visibility, 'typeHint' => $typeHint, 'columnPhpName' => $column->getPhpName(), 'localeColumnName' => $this->behavior->getLocaleColumn()->getPhpName()]); }
/** * @param \Propel\Generator\Model\Column $col * * @return string */ protected function addFilterByColIn(Column $col) { $script = ''; if ($col->isNumericType() || $col->isTemporalType() || $col->getType() == PropelTypes::ENUM || $col->isTextType()) { $colPhpName = $col->getPhpName(); $variableName = $col->getCamelCaseName(); $queryClassName = $this->getQueryClassName(); $script .= <<<SCRIPT /** * Applies Criteria::IN filtering criteria for the column. * * @param array \${$variableName}s Filter value. * * @return \$this|{$queryClassName} The current query, for fluid interface */ public function filterBy{$colPhpName}_In(array \${$variableName}s) { return \$this->filterBy{$colPhpName}(\${$variableName}s, Criteria::IN); } SCRIPT; } return $script; }
/** * Get the PHP snippet for binding a value to a column. * Warning: duplicates logic from OracleAdapter::bindValue(). * Any code modification here must be ported there. */ public function getColumnBindingPHP(Column $column, $identifier, $columnValueAccessor, $tab = " ") { if ($column->getPDOType() == PropelTypes::CLOB_EMU) { return sprintf("%s\$stmt->bindParam(%s, %s, %s, strlen(%s));\n", $tab, $identifier, $columnValueAccessor, PropelTypes::getPdoTypeString($column->getType()), $columnValueAccessor); } return parent::getColumnBindingPHP($column, $identifier, $columnValueAccessor, $tab); }
/** * Adds a remove method for an array column. * @param string &$script The script will be modified in this method. * @param Column $col The current column. */ protected function addRemoveArrayElement(&$script, Column $col) { $clo = $col->getLowercasedName(); $cfc = $col->getPhpName(); $visibility = $col->getAccessorVisibility(); $singularPhpName = $col->getPhpSingularName(); $columnType = $col->getType() === PropelTypes::PHP_ARRAY ? 'array' : 'set'; $script .= "\n /**\n * Removes a value from the [{$clo}] {$columnType} column value.\n * @param mixed \$value\n * " . $col->getDescription(); if ($col->isLazyLoad()) { $script .= "\n * @param ConnectionInterface \$con An optional ConnectionInterface connection to use for fetching this lazy-loaded column."; } $script .= "\n * @return \$this|" . $this->getObjectClassName(true) . " The current object (for fluent API support)\n */\n {$visibility} function remove{$singularPhpName}(\$value"; if ($col->isLazyLoad()) { $script .= ", ConnectionInterface \$con = null"; } // we want to reindex the array, so array_ functions are not the best choice $script .= ")\n {\n \$targetArray = array();\n foreach (\$this->get{$cfc}("; if ($col->isLazyLoad()) { $script .= "\$con"; } $script .= ") as \$element) {\n if (\$element != \$value) {\n \$targetArray []= \$element;\n }\n }\n \$this->set{$cfc}(\$targetArray);\n\n return \$this;\n } // remove{$singularPhpName}()\n"; }
public function getColumnDDL(Column $col) { $domain = $col->getDomain(); $ddl = array($this->quoteIdentifier($col->getName())); $sqlType = $domain->getSqlType(); $table = $col->getTable(); if ($col->isAutoIncrement() && $table && $table->getIdMethodParameters() == null) { $sqlType = $col->getType() === PropelTypes::BIGINT ? 'bigserial' : 'serial'; } if ($this->hasSize($sqlType) && $col->isDefaultSqlType($this)) { $ddl[] = $sqlType . $domain->printSize(); } else { $ddl[] = $sqlType; } if ($default = $this->getColumnDefaultValueDDL($col)) { $ddl[] = $default; } if ($notNull = $this->getNullString($col->isNotNull())) { $ddl[] = $notNull; } if ($autoIncrement = $col->getAutoIncrementString()) { $ddl[] = $autoIncrement; } return implode(' ', $ddl); }
/** * Get the PHP snippet for binding a value to a column. * Warning: duplicates logic from AdapterInterface::bindValue(). * Any code modification here must be ported there. */ public function getColumnBindingPHP(Column $column, $identifier, $columnValueAccessor, $tab = " ") { $script = ''; if ($column->isTemporalType()) { $columnValueAccessor = $columnValueAccessor . " ? " . $columnValueAccessor . "->format(\"" . $this->getTimeStampFormatter() . "\") : null"; } elseif ($column->isLobType()) { // we always need to make sure that the stream is rewound, otherwise nothing will // get written to database. $script .= "\nif (is_resource({$columnValueAccessor})) {\n rewind({$columnValueAccessor});\n}"; } $script .= sprintf("\n\$stmt->bindValue(%s, %s, %s);", $identifier, $columnValueAccessor, PropelTypes::getPdoTypeString($column->getType())); return preg_replace('/^(.+)/m', $tab . '$1', $script); }
/** * Adds the function body for the lazy loader method. * * @param string &$script * @param Column $column */ protected function addLazyLoaderBody(&$script, Column $column) { $platform = $this->getPlatform(); $clo = $column->getLowercasedName(); // pdo_sqlsrv driver requires the use of PDOStatement::bindColumn() or a hex string will be returned if ($column->getType() === PropelTypes::BLOB && $platform instanceof SqlsrvPlatform) { $script .= "\n \$c = \$this->buildPkeyCriteria();\n \$c->addSelectColumn(" . $this->getColumnConstant($column) . ");\n try {\n \$row = array(0 => null);\n \$dataFetcher = " . $this->getQueryClassName() . "::create(null, \$c)->setFormatter(ModelCriteria::FORMAT_STATEMENT)->find(\$con);\n if (\$dataFetcher instanceof PDODataFetcher) {\n \$dataFetcher->bindColumn(1, \$row[0], PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);\n }\n \$row = \$dataFetcher->fetch(PDO::FETCH_BOUND);\n \$dataFetcher->close();"; } else { $script .= "\n \$c = \$this->buildPkeyCriteria();\n \$c->addSelectColumn(" . $this->getColumnConstant($column) . ");\n try {\n \$dataFetcher = " . $this->getQueryClassName() . "::create(null, \$c)->setFormatter(ModelCriteria::FORMAT_STATEMENT)->find(\$con);\n \$row = \$dataFetcher->fetch();\n \$dataFetcher->close();"; } $script .= "\n\n \$firstColumn = \$row ? current(\$row) : null;\n"; if ($column->getType() === PropelTypes::CLOB && $platform instanceof OraclePlatform) { // PDO_OCI returns a stream for CLOB objects, while other PDO adapters return a string... $script .= "\n if (\$firstColumn) {\n \$this->{$clo} = stream_get_contents(\$firstColumn);\n }"; } elseif ($column->isLobType() && !$platform->hasStreamBlobImpl()) { $script .= "\n if (\$firstColumn !== null) {\n \$this->{$clo} = fopen('php://memory', 'r+');\n fwrite(\$this->{$clo}, \$firstColumn);\n rewind(\$this->{$clo});\n } else {\n \$this->{$clo} = null;\n }"; } elseif ($column->isPhpPrimitiveType()) { $script .= "\n \$this->{$clo} = (\$firstColumn !== null) ? (" . $column->getPhpType() . ") \$firstColumn : null;"; } elseif ($column->isPhpObjectType()) { $script .= "\n \$this->{$clo} = (\$firstColumn !== null) ? new " . $column->getPhpType() . "(\$firstColumn) : null;"; } else { $script .= "\n \$this->{$clo} = \$firstColumn;"; } $script .= "\n \$this->" . $clo . "_isLoaded = true;\n } catch (Exception \$e) {\n throw new PropelException(\"Error loading value for [{$clo}] column on demand.\", 0, \$e);\n }"; }
/** * Returns the SQL for the default value of a Column object * @return string */ public function getColumnDefaultValueDDL(Column $col) { $default = ''; $defaultValue = $col->getDefaultValue(); if ($defaultValue !== null) { $default .= 'DEFAULT '; if ($defaultValue->isExpression()) { $default .= $defaultValue->getValue(); } else { if ($col->isTextType()) { $default .= $this->quote($defaultValue->getValue()); } elseif ($col->getType() == PropelTypes::BOOLEAN || $col->getType() == PropelTypes::BOOLEAN_EMU) { $default .= $this->getBooleanString($defaultValue->getValue()); } elseif ($col->getType() == PropelTypes::ENUM) { $default .= array_search($defaultValue->getValue(), $col->getValueSet()); } else { $default .= $defaultValue->getValue(); } } } return $default; }
protected function addTranslatedColumnSetter(Column $column) { $i18nTablePhpName = $this->builder->getNewStubObjectBuilder($this->behavior->getI18nTable())->getClassname(); $tablePhpName = $this->builder->getStubObjectBuilder()->getClassname(); $objectBuilder = $this->builder->getNewObjectBuilder($this->behavior->getI18nTable()); $comment = ''; $functionStatement = ''; if ($column->getType() === PropelTypes::DATE || $column->getType() === PropelTypes::TIME || $column->getType() === PropelTypes::TIMESTAMP) { $objectBuilder->addTemporalMutatorComment($comment, $column); $objectBuilder->addMutatorOpenOpen($functionStatement, $column); } else { $objectBuilder->addMutatorComment($comment, $column); $objectBuilder->addMutatorOpenOpen($functionStatement, $column); } $comment = preg_replace('/^\\t/m', '', $comment); $comment = str_replace('@return ' . $i18nTablePhpName, '@return ' . $tablePhpName, $comment); $functionStatement = preg_replace('/^\\t/m', '', $functionStatement); preg_match_all('/\\$[a-z]+/i', $functionStatement, $params); return $this->behavior->renderTemplate('objectTranslatedColumnSetter', array('comment' => $comment, 'functionStatement' => $functionStatement, 'columnPhpName' => $column->getPhpName(), 'params' => implode(', ', $params[0]))); }
/** * Adds the function body for the lazy loader method * @param string &$script The script will be modified in this method. * @param Column $col The current column. * @see addLazyLoader() **/ protected function addLazyLoaderBody(&$script, Column $col) { $platform = $this->getPlatform(); $clo = strtolower($col->getName()); // pdo_sqlsrv driver requires the use of PDOStatement::bindColumn() or a hex string will be returned if ($col->getType() === PropelTypes::BLOB && $platform instanceof SqlsrvPlatform) { $script .= "\n \$c = \$this->buildPkeyCriteria();\n \$c->addSelectColumn(" . $this->getColumnConstant($col) . ");\n try {\n \$row = array(0 => null);\n \$stmt = " . $this->getPeerClassname() . "::doSelectStmt(\$c, \$con);\n \$stmt->bindColumn(1, \$row[0], PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);\n \$stmt->fetch(PDO::FETCH_BOUND);\n \$stmt->closeCursor();"; } else { $script .= "\n \$c = \$this->buildPkeyCriteria();\n \$c->addSelectColumn(" . $this->getColumnConstant($col) . ");\n try {\n \$stmt = " . $this->getPeerClassname() . "::doSelectStmt(\$c, \$con);\n \$row = \$stmt->fetch(PDO::FETCH_NUM);\n \$stmt->closeCursor();"; } if ($col->getType() === PropelTypes::CLOB && $platform instanceof OraclePlatform) { // PDO_OCI returns a stream for CLOB objects, while other PDO adapters return a string... $script .= "\n \$this->{$clo} = stream_get_contents(\$row[0]);"; } elseif ($col->isLobType() && !$platform->hasStreamBlobImpl()) { $script .= "\n if (\$row[0] !== null) {\n \$this->{$clo} = fopen('php://memory', 'r+');\n fwrite(\$this->{$clo}, \$row[0]);\n rewind(\$this->{$clo});\n } else {\n \$this->{$clo} = null;\n }"; } elseif ($col->isPhpPrimitiveType()) { $script .= "\n \$this->{$clo} = (\$row[0] !== null) ? (" . $col->getPhpType() . ") \$row[0] : null;"; } elseif ($col->isPhpObjectType()) { $script .= "\n \$this->{$clo} = (\$row[0] !== null) ? new " . $col->getPhpType() . "(\$row[0]) : null;"; } else { $script .= "\n \$this->{$clo} = \$row[0];"; } $script .= "\n \$this->" . $clo . "_isLoaded = true;\n } catch (Exception \$e) {\n throw new PropelException(\"Error loading value for [{$clo}] column on demand.\", 0, \$e);\n }"; }
/** * Appends the generated <column> XML node to its parent node. * * @param Column $column The Column model instance * @param \DOMNode $parentNode The parent DOMNode object */ private function appendColumnNode(Column $column, \DOMNode $parentNode) { $columnNode = $parentNode->appendChild($this->document->createElement('column')); $columnNode->setAttribute('name', $column->getName()); if ($phpName = $column->getPhpName()) { $columnNode->setAttribute('phpName', $phpName); } $columnNode->setAttribute('type', $column->getType()); $domain = $column->getDomain(); if ($size = $domain->getSize()) { $columnNode->setAttribute('size', $size); } if (null !== ($scale = $domain->getScale())) { $columnNode->setAttribute('scale', $scale); } $platform = $column->getPlatform(); if ($platform && !$column->isDefaultSqlType($platform)) { $columnNode->setAttribute('sqlType', $domain->getSqlType()); } if ($description = $column->getDescription()) { $columnNode->setAttribute('description', $description); } if ($column->isPrimaryKey()) { $columnNode->setAttribute('primaryKey', 'true'); } if ($column->isAutoIncrement()) { $columnNode->setAttribute('autoIncrement', 'true'); } if ($column->isNotNull()) { $columnNode->setAttribute('required', 'true'); } $defaultValue = $domain->getDefaultValue(); if ($defaultValue) { $type = $defaultValue->isExpression() ? 'defaultExpr' : 'defaultValue'; $columnNode->setAttribute($type, $defaultValue->getValue()); } if ($column->isInheritance()) { $columnNode->setAttribute('inheritance', $column->getInheritanceType()); foreach ($column->getInheritanceList() as $inheritance) { $this->appendInheritanceNode($inheritance, $columnNode); } } if ($column->isNodeKey()) { $columnNode->setAttribute('nodeKey', 'true'); if ($nodeKeySeparator = $column->getNodeKeySep()) { $columnNode->setAttribute('nodeKeySep', $nodeKeySeparator); } } foreach ($column->getVendorInformation() as $vendorInformation) { $this->appendVendorInformationNode($vendorInformation, $columnNode); } }
/** * 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->isPhpArrayType()) { $value = $this->getPhpArrayString($defaultValue->getValue()); if (null === $value) { $default = ''; } else { $default .= $value; } } else { $default .= $defaultValue->getValue(); } } } return $default; }