Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
6659 lines (5916 sloc) 227 KB
<?php
/**
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\Generator\Builder\Om;
use Propel\Common\Util\SetColumnConverter;
use Propel\Generator\Exception\EngineException;
use Propel\Generator\Model\Column;
use Propel\Generator\Model\CrossForeignKeys;
use Propel\Generator\Model\ForeignKey;
use Propel\Generator\Model\IdMethod;
use Propel\Generator\Model\PropelTypes;
use Propel\Generator\Model\Table;
use Propel\Generator\Platform\MssqlPlatform;
use Propel\Generator\Platform\MysqlPlatform;
use Propel\Generator\Platform\OraclePlatform;
use Propel\Generator\Platform\PlatformInterface;
use Propel\Generator\Platform\SqlsrvPlatform;
use Propel\Runtime\Exception\PropelException;
/**
* Generates a PHP5 base Object class for user object model (OM).
*
* This class produces the base object class (e.g. BaseMyTable) which contains
* all the custom-built accessor and setter methods.
*
* @author Hans Lellelid <hans@xmpl.org>
*/
class ObjectBuilder extends AbstractObjectBuilder
{
/**
* Returns the package for the base object classes.
*
* @return string
*/
public function getPackage()
{
return parent::getPackage() . ".Base";
}
/**
* Returns the namespace for the base class.
*
* @return string
* @see Propel\Generator\Builder\Om.AbstractOMBuilder::getNamespace()
*/
public function getNamespace()
{
if ($namespace = parent::getNamespace()) {
return $namespace . '\\Base';
}
return 'Base';
}
/**
* Returns default key type.
*
* If not presented in configuration default will be 'TYPE_PHPNAME'
*
* @return string
*/
public function getDefaultKeyType()
{
$defaultKeyType = $this->getBuildProperty('generator.objectModel.defaultKeyType') ? $this->getBuildProperty('generator.objectModel.defaultKeyType') : 'phpName';
return "TYPE_".strtoupper($defaultKeyType);
}
/**
* Returns the name of the current class being built.
*
* @return string
*/
public function getUnprefixedClassName()
{
return $this->getStubObjectBuilder()->getUnprefixedClassName();
}
/**
* Validates the current table to make sure that it won't result in
* generated code that will not parse.
*
* This method may emit warnings for code which may cause problems
* and will throw exceptions for errors that will definitely cause
* problems.
*/
protected function validateModel()
{
parent::validateModel();
$table = $this->getTable();
// Check to see whether any generated foreign key names
// will conflict with column names.
$colPhpNames = [];
$fkPhpNames = [];
foreach ($table->getColumns() as $col) {
$colPhpNames[] = $col->getPhpName();
}
foreach ($table->getForeignKeys() as $fk) {
$fkPhpNames[] = $this->getFKPhpNameAffix($fk, false);
}
$intersect = array_intersect($colPhpNames, $fkPhpNames);
if (!empty($intersect)) {
throw new EngineException("One or more of your column names for [" . $table->getName() . "] table conflict with foreign key names (" . implode(", ", $intersect) . ")");
}
// Check foreign keys to see if there are any foreign keys that
// are also matched with an inversed referencing foreign key
// (this is currently unsupported behavior)
// see: http://propel.phpdb.org/trac/ticket/549
foreach ($table->getForeignKeys() as $fk) {
if ($fk->isMatchedByInverseFK()) {
throw new EngineException(sprintf('The 1:1 relationship expressed by foreign key %s is defined in both directions; Propel does not currently support this (if you must have both foreign key constraints, consider adding this constraint with a custom SQL file.)', $fk->getName()));
}
}
}
/**
* Returns the appropriate formatter (from platform) for a date/time column.
*
* @param Column $column
* @return string
*/
protected function getTemporalFormatter(Column $column)
{
$fmt = null;
if ($column->getType() === PropelTypes::DATE) {
$fmt = $this->getPlatform()->getDateFormatter();
} elseif ($column->getType() === PropelTypes::TIME) {
$fmt = $this->getPlatform()->getTimeFormatter();
} elseif ($column->getType() === PropelTypes::TIMESTAMP) {
$fmt = $this->getPlatform()->getTimestampFormatter();
}
return $fmt;
}
/**
* 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;
}
/**
* Adds class phpdoc comment and opening of class.
*
* @param string &$script
*/
protected function addClassOpen(&$script)
{
$table = $this->getTable();
$tableName = $table->getName();
$tableDesc = $table->getDescription();
if (null !== ($parentClass = $this->getBehaviorContent('parentClass')) ||
null !== ($parentClass = ClassTools::classname($this->getBaseClass()))) {
$parentClass = ' extends '.$parentClass;
}
if ($this->getBuildProperty('generator.objectModel.addClassLevelComment')) {
$script .= "
/**
* Base class that represents a row from the '$tableName' table.
*
* $tableDesc
*";
if ($this->getBuildProperty('generator.objectModel.addTimeStamp')) {
$now = strftime('%c');
$script .= "
* This class was autogenerated by Propel " . $this->getBuildProperty('general.version') . " on:
*
* $now
*";
}
$script .= "
* @package propel.generator.".$this->getPackage()."
*/";
}
$script .= "
abstract class ".$this->getUnqualifiedClassName().$parentClass." implements ActiveRecordInterface ";
if ($interface = $this->getInterface()) {
$script .= ", Child" . ClassTools::classname($interface);
if ($interface !== ClassTools::classname($interface)) {
$this->declareClass($interface);
} else {
$this->declareClassFromBuilder($this->getInterfaceBuilder());
}
}
$script .= "
{";
}
/**
* Specifies the methods that are added as part of the basic OM class.
* This can be overridden by subclasses that wish to add more methods.
*
* @param string &$script
* @see ObjectBuilder::addClassBody()
*/
protected function addClassBody(&$script)
{
$this->declareClassFromBuilder($this->getStubObjectBuilder());
$this->declareClassFromBuilder($this->getStubQueryBuilder());
$this->declareClassFromBuilder($this->getTableMapBuilder());
$this->declareClasses(
'\Exception',
'\PDO',
'\Propel\Runtime\Exception\PropelException',
'\Propel\Runtime\Connection\ConnectionInterface',
'\Propel\Runtime\Collection\Collection',
'\Propel\Runtime\Collection\ObjectCollection',
'\Propel\Runtime\Collection\ObjectCombinationCollection',
'\Propel\Runtime\Exception\BadMethodCallException',
'\Propel\Runtime\Exception\PropelException',
'\Propel\Runtime\ActiveQuery\Criteria',
'\Propel\Runtime\ActiveQuery\ModelCriteria',
'\Propel\Runtime\ActiveRecord\ActiveRecordInterface',
'\Propel\Runtime\Parser\AbstractParser',
'\Propel\Runtime\Propel',
'\Propel\Runtime\Map\TableMap'
);
$baseClass = $this->getBaseClass();
if (strrpos($baseClass, '\\') !== false) {
$this->declareClasses($baseClass);
}
$table = $this->getTable();
if (!$table->isAlias()) {
$this->addConstants($script);
$this->addAttributes($script);
}
if ($table->hasCrossForeignKeys()) {
/* @var $refFK ForeignKey */
foreach ($table->getCrossFks() as $crossFKs) {
$this->addCrossScheduledForDeletionAttribute($script, $crossFKs);
}
}
foreach ($table->getReferrers() as $refFK) {
if (!$refFK->isLocalPrimaryKey()) {
$this->addRefFkScheduledForDeletionAttribute($script, $refFK);
}
}
if ($this->hasDefaultValues()) {
$this->addApplyDefaultValues($script);
}
$this->addConstructor($script);
$this->addBaseObjectMethods($script);
$this->addColumnAccessorMethods($script);
$this->addColumnMutatorMethods($script);
$this->addHasOnlyDefaultValues($script);
$this->addHydrate($script);
$this->addEnsureConsistency($script);
if (!$table->isReadOnly()) {
$this->addManipulationMethods($script);
}
if ($this->isAddGenericAccessors()) {
$this->addGetByName($script);
$this->addGetByPosition($script);
$this->addToArray($script);
}
if ($this->isAddGenericMutators()) {
$this->addSetByName($script);
$this->addSetByPosition($script);
$this->addFromArray($script);
$this->addImportFrom($script);
}
$this->addBuildCriteria($script);
$this->addBuildPkeyCriteria($script);
$this->addHashCode($script);
$this->addGetPrimaryKey($script);
$this->addSetPrimaryKey($script);
$this->addIsPrimaryKeyNull($script);
$this->addCopy($script);
$this->addFKMethods($script);
$this->addRefFKMethods($script);
$this->addCrossFKMethods($script);
$this->addClear($script);
$this->addClearAllReferences($script);
$this->addPrimaryString($script);
// apply behaviors
$this->applyBehaviorModifier('objectMethods', $script, " ");
if ($this->getBuildProperty('generator.objectModel.addHooks')) {
$this->addHookMethods($script);
}
$this->addMagicCall($script);
}
/**
* Closes class.
*
* @param string &$script
*/
protected function addClassClose(&$script)
{
$script .= "
}
";
$this->applyBehaviorModifier('objectFilter', $script, "");
}
/**
* Adds any constants to the class.
*
* @param string &$script
*/
protected function addConstants(&$script)
{
$script .= "
/**
* TableMap class name
*/
const TABLE_MAP = '" . addslashes($this->getTableMapBuilder()->getFullyQualifiedClassName()) . "';
";
}
/**
* Adds class attributes.
*
* @param string &$script
*/
protected function addAttributes(&$script)
{
$table = $this->getTable();
$script .= "
";
$script .= $this->renderTemplate('baseObjectAttributes');
if (!$table->isAlias()) {
$this->addColumnAttributes($script);
}
foreach ($table->getForeignKeys() as $fk) {
$this->addFKAttributes($script, $fk);
}
foreach ($table->getReferrers() as $refFK) {
$this->addRefFKAttributes($script, $refFK);
}
// many-to-many relationships
foreach ($table->getCrossFks() as $crossFKs) {
$this->addCrossFKAttributes($script, $crossFKs);
}
$this->addAlreadyInSaveAttribute($script);
// apply behaviors
$this->applyBehaviorModifier('objectAttributes', $script, " ");
}
/**
* Adds variables that store column values.
*
* @param string &$script
*/
protected function addColumnAttributes(&$script)
{
$table = $this->getTable();
foreach ($table->getColumns() as $col) {
$this->addColumnAttributeComment($script, $col);
$this->addColumnAttributeDeclaration($script, $col);
if ($col->isLazyLoad() ) {
$this->addColumnAttributeLoaderComment($script, $col);
$this->addColumnAttributeLoaderDeclaration($script, $col);
}
if ($col->getType() == PropelTypes::OBJECT || $col->getType() == PropelTypes::PHP_ARRAY) {
$this->addColumnAttributeUnserializedComment($script, $col);
$this->addColumnAttributeUnserializedDeclaration($script, $col);
}
if ($col->isSetType()) {
$this->addColumnAttributeConvertedDeclaration($script, $col);
}
}
}
/**
* Adds comment about the attribute (variable) that stores column values.
*
* @param string &$script
* @param Column $column
*/
protected function addColumnAttributeComment(&$script, Column $column)
{
if ($column->isTemporalType()) {
$cptype = $this->getDateTimeClass($column);
} else {
$cptype = $column->getPhpType();
}
$clo = $column->getLowercasedName();
$script .= "
/**
* The value for the $clo field.
* ".$column->getDescription();
if ($column->getDefaultValue()) {
if ($column->getDefaultValue()->isExpression()) {
$script .= "
* Note: this column has a database default value of: (expression) ".$column->getDefaultValue()->getValue();
} else {
$script .= "
* Note: this column has a database default value of: ". $this->getDefaultValueString($column);
}
}
$script .= "
* @var $cptype
*/";
}
/**
* Adds the declaration of a column value storage attribute.
*
* @param string &$script
* @param Column $column
*/
protected function addColumnAttributeDeclaration(&$script, Column $column)
{
$clo = $column->getLowercasedName();
$script .= "
protected \$" . $clo . ";
";
}
/**
* Adds the comment about the attribute keeping track if an attribute value
* has been loaded.
*
* @param string &$script
* @param Column $column
*/
protected function addColumnAttributeLoaderComment(&$script, Column $column)
{
$clo = $column->getLowercasedName();
$script .= "
/**
* Whether the lazy-loaded \$$clo value has been loaded from database.
* This is necessary to avoid repeated lookups if \$$clo column is NULL in the db.
* @var boolean
*/";
}
/**
* Adds the declaration of the attribute keeping track of an attribute
* loaded state.
*
* @param string &$script
* @param Column $column
*/
protected function addColumnAttributeLoaderDeclaration(&$script, Column $column)
{
$clo = $column->getLowercasedName();
$script .= "
protected \$".$clo."_isLoaded = false;
";
}
/**
* Adds the comment about the serialized attribute.
*
* @param string &$script
* @param Column $column
*/
protected function addColumnAttributeUnserializedComment(&$script, Column $column)
{
$clo = $column->getLowercasedName();
$script .= "
/**
* The unserialized \$$clo value - i.e. the persisted object.
* This is necessary to avoid repeated calls to unserialize() at runtime.
* @var object
*/";
}
/**
* Adds the declaration of the serialized attribute.
*
* @param string &$script
* @param Column $column
*/
protected function addColumnAttributeUnserializedDeclaration(&$script, Column $column)
{
$clo = $column->getLowercasedName() . "_unserialized";
$script .= "
protected \$" . $clo . ";
";
}
/**
* @param string &$script
* @param Column $column
*/
protected function addColumnAttributeConvertedDeclaration(&$script, Column $column)
{
$clo = $column->getLowercasedName() . "_converted";
$script .= "
protected \$" . $clo . ";
";
}
/**
* Adds the constructor for this object.
*
* @param string &$script
*/
protected function addConstructor(&$script)
{
$this->addConstructorComment($script);
$this->addConstructorOpen($script);
if ($this->hasDefaultValues()) {
$this->addConstructorBody($script);
}
$this->addConstructorClose($script);
}
/**
* Adds the comment for the constructor
*
* @param string &$script
*/
protected function addConstructorComment(&$script)
{
$script .= "
/**
* Initializes internal state of ".$this->getQualifiedClassName()." object.";
if ($this->hasDefaultValues()) {
$script .= "
* @see applyDefaults()";
}
$script .= "
*/";
}
/**
* Adds the function declaration for the constructor.
*
* @param string &$script
*/
protected function addConstructorOpen(&$script)
{
$script .= "
public function __construct()
{";
}
/**
* Adds the function body for the constructor.
*
* @param string &$script
*/
protected function addConstructorBody(&$script)
{
$script .= "
\$this->applyDefaultValues();";
}
/**
* Adds the function close for the constructor.
*
* @param string &$script
*/
protected function addConstructorClose(&$script)
{
$script .= "
}
";
}
/**
* Adds the base object functions.
*
* @param string &$script
*/
protected function addBaseObjectMethods(&$script)
{
$script .= $this->renderTemplate('baseObjectMethods', ['className' => $this->getUnqualifiedClassName()]);
}
/**
* Adds the base object hook functions.
*
* @param string &$script
*/
protected function addHookMethods(&$script)
{
$hooks = [];
foreach (['pre', 'post'] as $hook) {
foreach (['Insert', 'Update', 'Save', 'Delete'] as $action) {
$hooks[$hook.$action] = false === strpos($script, "function $hook.$action(");
}
}
$script .= $this->renderTemplate('baseObjectMethodHook', $hooks);
}
/**
* Adds the applyDefaults() method, which is called from the constructor.
*
* @param string &$script
*/
protected function addApplyDefaultValues(&$script)
{
$this->addApplyDefaultValuesComment($script);
$this->addApplyDefaultValuesOpen($script);
$this->addApplyDefaultValuesBody($script);
$this->addApplyDefaultValuesClose($script);
}
/**
* Adds the comment for the applyDefaults method.
*
* @param string &$script
*/
protected function addApplyDefaultValuesComment(&$script)
{
$script .= "
/**
* Applies default values to this object.
* This method should be called from the object's constructor (or
* equivalent initialization method).
* @see __construct()
*/";
}
/**
* Adds the function declaration for the applyDefaults method.
*
* @param string &$script
*/
protected function addApplyDefaultValuesOpen(&$script)
{
$script .= "
public function applyDefaultValues()
{";
}
/**
* Adds the function body of the applyDefault method.
*
* @param string &$script
*/
protected function addApplyDefaultValuesBody(&$script)
{
$table = $this->getTable();
// FIXME - Apply support for PHP default expressions here
// see: http://propel.phpdb.org/trac/ticket/378
$colsWithDefaults = [];
foreach ($table->getColumns() as $column) {
$def = $column->getDefaultValue();
if ($def !== null && !$def->isExpression()) {
$colsWithDefaults[] = $column;
}
}
foreach ($colsWithDefaults as $column) {
/** @var Column $column */
$clo = $column->getLowercasedName();
$defaultValue = $this->getDefaultValueString($column);
if ($column->isTemporalType()) {
$dateTimeClass = $this->getDateTimeClass($column);
$script .= "
\$this->".$clo." = PropelDateTime::newInstance($defaultValue, null, '$dateTimeClass');";
} else {
$script .= "
\$this->".$clo." = $defaultValue;";
}
}
}
/**
* Adds the function close for the applyDefaults method.
*
* @param string &$script
*/
protected function addApplyDefaultValuesClose(&$script)
{
$script .= "
}
";
}
/**
* Adds a date/time/timestamp getter method.
*
* @param string &$script
* @param Column $column
*/
protected function addTemporalAccessor(&$script, Column $column)
{
$this->addTemporalAccessorComment($script, $column);
$this->addTemporalAccessorOpen($script, $column);
$this->addTemporalAccessorBody($script, $column);
$this->addTemporalAccessorClose($script);
}
/**
* Adds the comment for a temporal accessor.
*
* @param string &$script
* @param Column $column
*/
public function addTemporalAccessorComment(&$script, Column $column)
{
$clo = $column->getLowercasedName();
$dateTimeClass = $this->getDateTimeClass($column);
$handleMysqlDate = false;
if ($this->getPlatform() instanceof MysqlPlatform) {
if ($column->getType() === PropelTypes::TIMESTAMP) {
$handleMysqlDate = true;
$mysqlInvalidDateString = '0000-00-00 00:00:00';
} elseif ($column->getType() === PropelTypes::DATE) {
$handleMysqlDate = true;
$mysqlInvalidDateString = '0000-00-00';
}
// 00:00:00 is a valid time, so no need to check for that.
}
$script .= "
/**
* Get the [optionally formatted] temporal [$clo] column value.
* {$column->getDescription()}
*
* @param string|null \$format The date/time format string (either date()-style or strftime()-style).
* If format is NULL, then the raw $dateTimeClass object will be returned.
*
* @return string|$dateTimeClass Formatted date/time value as string or $dateTimeClass object (if format is NULL), NULL if column is NULL" .($handleMysqlDate ? ', and 0 if column value is ' . $mysqlInvalidDateString : '')."
*
* @throws PropelException - if unable to parse/validate the date/time value.
*/";
}
/**
* Adds the function declaration for a temporal accessor.
*
* @param string &$script
* @param Column $column
*/
public function addTemporalAccessorOpen(&$script, Column $column)
{
$cfc = $column->getPhpName();
$defaultfmt = null;
$visibility = $column->getAccessorVisibility();
// Default date/time formatter strings are specified in propel config
if ($column->getType() === PropelTypes::DATE) {
$defaultfmt = $this->getBuildProperty('generator.dateTime.defaultDateFormat');
} elseif ($column->getType() === PropelTypes::TIME) {
$defaultfmt = $this->getBuildProperty('generator.dateTime.defaultTimeFormat');
} elseif ($column->getType() === PropelTypes::TIMESTAMP) {
$defaultfmt = $this->getBuildProperty('generator.dateTime.defaultTimeStampFormat');
}
if (empty($defaultfmt)) {
$defaultfmt = null;
}
$script .= "
".$visibility." function get$cfc(\$format = ".var_export($defaultfmt, true)."";
if ($column->isLazyLoad()) {
$script .= ", \$con = null";
}
$script .= ")
{";
}
/**
* Gets accessor lazy loaded snippets.
*
* @param Column $column
* @return string
*/
protected function getAccessorLazyLoadSnippet(Column $column)
{
if ($column->isLazyLoad()) {
$clo = $column->getLowercasedName();
$defaultValueString = 'null';
$def = $column->getDefaultValue();
if ($def !== null && !$def->isExpression()) {
$defaultValueString = $this->getDefaultValueString($column);
}
return "
if (!\$this->{$clo}_isLoaded && \$this->{$clo} === {$defaultValueString} && !\$this->isNew()) {
\$this->load{$column->getPhpName()}(\$con);
}
";
}
return '';
}
/**
* Adds the body of the temporal accessor.
*
* @param string &$script
* @param Column $column
*/
protected function addTemporalAccessorBody(&$script, Column $column)
{
$clo = $column->getLowercasedName();
$dateTimeClass = $this->getDateTimeClass($column);
$this->declareClasses($dateTimeClass);
$defaultfmt = null;
// Default date/time formatter strings are specified in propel config
if ($column->getType() === PropelTypes::DATE) {
$defaultfmt = $this->getBuildProperty('generator.dateTime.defaultDateFormat');
} elseif ($column->getType() === PropelTypes::TIME) {
$defaultfmt = $this->getBuildProperty('generator.dateTime.defaultTimeFormat');
} elseif ($column->getType() === PropelTypes::TIMESTAMP) {
$defaultfmt = $this->getBuildProperty('generator.dateTime.defaultTimeStampFormat');
}
if (empty($defaultfmt)) {
$defaultfmt = null;
}
if ($column->isLazyLoad()) {
$script .= $this->getAccessorLazyLoadSnippet($column);
}
$script .= "
if (\$format === null) {
return \$this->$clo;
} else {
return \$this->$clo instanceof \DateTimeInterface ? \$this->{$clo}->format(\$format) : null;
}";
}
/**
* Adds the body of the temporal accessor.
*
* @param string &$script
*/
protected function addTemporalAccessorClose(&$script)
{
$script .= "
}
";
}
/**
* Adds an object getter method.
*
* @param string &$script
* @param Column $column
*/
protected function addObjectAccessor(&$script, Column $column)
{
$this->addDefaultAccessorComment($script, $column);
$this->addDefaultAccessorOpen($script, $column);
$this->addObjectAccessorBody($script, $column);
$this->addDefaultAccessorClose($script);
}
/**
* Adds the function body for an object accessor method.
*
* @param string &$script
* @param Column $column
*/
protected function addObjectAccessorBody(&$script, Column $column)
{
$clo = $column->getLowercasedName();
$cloUnserialized = $clo.'_unserialized';
if ($column->isLazyLoad()) {
$script .= $this->getAccessorLazyLoadSnippet($column);
}
$script .= "
if (null == \$this->$cloUnserialized && is_resource(\$this->$clo)) {
if (\$serialisedString = stream_get_contents(\$this->$clo)) {
\$this->$cloUnserialized = unserialize(\$serialisedString);
}
}
return \$this->$cloUnserialized;";
}
protected function addJsonAccessor(&$script, Column $column)
{
$this->addJsonAccessorComment($script, $column);
$this->addJsonAccessorOpen($script, $column);
$this->addJsonAccessorBody($script, $column);
$this->addDefaultAccessorClose($script);
}
/**
* Add the comment for a json accessor method (a getter).
*
* @param string &$script
* @param Column $column
*/
public function addJsonAccessorComment(&$script, Column $column)
{
$clo=$column->getLowercasedName();
$script .= "
/**
* Get the [$clo] column value.
* ".$column->getDescription() ."
* @param bool \$asArray Returns the JSON data as array instead of object
";
if ($column->isLazyLoad()) {
$script .= "
* @param ConnectionInterface \$con An optional ConnectionInterface connection to use for fetching this lazy-loaded column.";
}
$script .= "
* @return object
*/";
}
/**
* Adds the function declaration for a JSON accessor.
*
* @param string &$script
* @param Column $column
*/
public function addJsonAccessorOpen(&$script, Column $column)
{
$cfc = $column->getPhpName();
$visibility = $column->getAccessorVisibility();
$script .= "
".$visibility." function get$cfc(\$asArray = true";
if ($column->isLazyLoad()) {
$script .= ", ConnectionInterface \$con = null";
}
$script .= ")
{";
}
protected function addJsonAccessorBody(&$script, Column $column)
{
$clo = $column->getLowercasedName();
$script .= "
return json_decode(\$this->$clo, \$asArray);";
}
/**
* Adds an array getter method.
*
* @param string &$script
* @param Column $column
*/
protected function addArrayAccessor(&$script, Column $column)
{
$this->addDefaultAccessorComment($script, $column);
$this->addDefaultAccessorOpen($script, $column);
$this->addArrayAccessorBody($script, $column);
$this->addDefaultAccessorClose($script);
}
/**
* Adds the function body for an array accessor method.
*
* @param string &$script
* @param Column $column
*/
protected function addArrayAccessorBody(&$script, Column $column)
{
$clo = $column->getLowercasedName();
$cloUnserialized = $clo.'_unserialized';
if ($column->isLazyLoad()) {
$script .= $this->getAccessorLazyLoadSnippet($column);
}
$script .= "
if (null === \$this->$cloUnserialized) {
\$this->$cloUnserialized = array();
}
if (!\$this->$cloUnserialized && null !== \$this->$clo) {
\$$cloUnserialized = substr(\$this->$clo, 2, -2);
\$this->$cloUnserialized = '' !== \$$cloUnserialized ? explode(' | ', \$$cloUnserialized) : array();
}
return \$this->$cloUnserialized;";
}
/**
* Adds a boolean isser method.
*
* @param string &$script
* @param Column $column
*/
protected function addBooleanAccessor(&$script, Column $column)
{
$name = self::getBooleanAccessorName($column);
if (in_array($name, ClassTools::getPropelReservedMethods())) {
//TODO: Issue a warning telling the user to use default accessors
return; // Skip boolean accessors for reserved names
}
$this->addDefaultAccessorComment($script, $column);
$this->addBooleanAccessorOpen($script, $column);
$this->addBooleanAccessorBody($script, $column);
$this->addDefaultAccessorClose($script);
}
/**
* Returns the name to be used as boolean accessor name
*
* @param Column $column
* @return string
*/
protected static function getBooleanAccessorName(Column $column)
{
$name = $column->getCamelCaseName();
if (!preg_match('/^(?:is|has)(?=[A-Z])/', $name)) {
$name = 'is' . ucfirst($name);
}
return $name;
}
/**
* Adds the function declaration for a boolean accessor.
*
* @param string &$script
* @param Column $column
*/
public function addBooleanAccessorOpen(&$script, Column $column)
{
$name = self::getBooleanAccessorName($column);
$visibility = $column->getAccessorVisibility();
$script .= "
".$visibility." function $name(";
if ($column->isLazyLoad()) {
$script .= "ConnectionInterface \$con = null";
}
$script .= ")
{";
}
/**
* Adds the function body for a boolean accessor method.
*
* @param string &$script
* @param Column $column
*/
protected function addBooleanAccessorBody(&$script, Column $column)
{
$cfc = $column->getPhpName();
$script .= "
return \$this->get$cfc(";
if ($column->isLazyLoad()) {
$script .= '$con';
}
$script .= ");";
}
/**
* Adds an enum getter method.
*
* @param string &$script
* @param Column $column
*/
protected function addEnumAccessor(&$script, Column $column)
{
$this->addEnumAccessorComment($script, $column);
$this->addDefaultAccessorOpen($script, $column);
$this->addEnumAccessorBody($script, $column);
$this->addDefaultAccessorClose($script);
}
/**
* Add the comment for an enum accessor method.
*
* @param string &$script
* @param Column $column
*/
public function addEnumAccessorComment(&$script, Column $column)
{
$clo=$column->getLowercasedName();
$script .= "
/**
* Get the [$clo] column value.
* ".$column->getDescription();
if ($column->isLazyLoad()) {
$script .= "
* @param ConnectionInterface An optional ConnectionInterface connection to use for fetching this lazy-loaded column.";
}
$script .= "
* @return string
* @throws \\Propel\\Runtime\\Exception\\PropelException
*/";
}
/**
* Adds the function body for an enum accessor method.
*
* @param string &$script
* @param Column $column
*/
protected function addEnumAccessorBody(&$script, Column $column)
{
$clo = $column->getLowercasedName();
if ($column->isLazyLoad()) {
$script .= $this->getAccessorLazyLoadSnippet($column);
}
$script .= "
if (null === \$this->$clo) {
return null;
}
\$valueSet = " . $this->getTableMapClassName() . "::getValueSet(" . $this->getColumnConstant($column) . ");
if (!isset(\$valueSet[\$this->$clo])) {
throw new PropelException('Unknown stored enum key: ' . \$this->$clo);
}
return \$valueSet[\$this->$clo];";
}
/**
* Adds a SET column getter method.
*
* @param string &$script
* @param Column $column
*/
protected function addSetAccessor(&$script, Column $column)
{
$this->addSetAccessorComment($script, $column);
$this->addDefaultAccessorOpen($script, $column);
$this->addSetAccessorBody($script, $column);
$this->addDefaultAccessorClose($script);
}
/**
* Add the comment for a SET column accessor method.
*
* @param string &$script
* @param Column $column
*/
public function addSetAccessorComment(&$script, Column $column)
{
$clo = $column->getLowercasedName();
$script .= "
/**
* Get the [$clo] column value.
* " . $column->getDescription();
if ($column->isLazyLoad()) {
$script .= "
* @param ConnectionInterface An optional ConnectionInterface connection to use for fetching this lazy-loaded column.";
}
$script .= "
* @return array|null
* @throws \\Propel\\Runtime\\Exception\\PropelException
*/";
}
/**
* Adds the function body for a SET column accessor method.
*
* @param string &$script
* @param Column $column
*/
protected function addSetAccessorBody(&$script, Column $column)
{
$clo = $column->getLowercasedName();
$cloConverted = $clo . '_converted';
if ($column->isLazyLoad()) {
$script .= $this->getAccessorLazyLoadSnippet($column);
}
$this->declareClasses(
'Propel\Common\Util\SetColumnConverter',
'Propel\Common\Exception\SetColumnConverterException'
);
$script .= "
if (null === \$this->$cloConverted) {
\$this->$cloConverted = array();
}
if (!\$this->$cloConverted && null !== \$this->$clo) {
\$valueSet = " . $this->getTableMapClassName() . "::getValueSet(" . $this->getColumnConstant($column) . ");
try {
\$this->$cloConverted = SetColumnConverter::convertIntToArray(\$this->$clo, \$valueSet);
} catch (SetColumnConverterException \$e) {
throw new PropelException('Unknown stored set key: ' . \$e->getValue(), \$e->getCode(), \$e);
}
}
return \$this->$cloConverted;";
}
/**
* Adds a tester method for an array column.
*
* @param string &$script
* @param Column $column
*/
protected function addHasArrayElement(&$script, Column $column)
{
$clo = $column->getLowercasedName();
$cfc = $column->getPhpName();
$visibility = $column->getAccessorVisibility();
$singularPhpName = $column->getPhpSingularName();
$columnType = ($column->getType() === PropelTypes::PHP_ARRAY) ? 'array' : 'set';
$script .= "
/**
* Test the presence of a value in the [$clo] $columnType column value.
* @param mixed \$value
* ".$column->getDescription();
if ($column->isLazyLoad()) {
$script .= "
* @param ConnectionInterface \$con An optional ConnectionInterface connection to use for fetching this lazy-loaded column.";
}
$script .= "
* @return boolean
*/
$visibility function has$singularPhpName(\$value";
if ($column->isLazyLoad()) {
$script .= ", ConnectionInterface \$con = null";
}
$script .= ")
{
return in_array(\$value, \$this->get$cfc(";
if ($column->isLazyLoad()) {
$script .= "\$con";
}
$script .= "));
} // has$singularPhpName()
";
}
/**
* Adds a normal (non-temporal) getter method.
*
* @param string &$script
* @param Column $column
*/
protected function addDefaultAccessor(&$script, Column $column)
{
$this->addDefaultAccessorComment($script, $column);
$this->addDefaultAccessorOpen($script, $column);
$this->addDefaultAccessorBody($script, $column);
$this->addDefaultAccessorClose($script);
}
/**
* Add the comment for a default accessor method (a getter).
*
* @param string &$script
* @param Column $column
*/
public function addDefaultAccessorComment(&$script, Column $column)
{
$clo=$column->getLowercasedName();
$script .= "
/**
* Get the [$clo] column value.
* ".$column->getDescription();
if ($column->isLazyLoad()) {
$script .= "
* @param ConnectionInterface \$con An optional ConnectionInterface connection to use for fetching this lazy-loaded column.";
}
$script .= "
* @return ".($column->getTypeHint() ?: ($column->getPhpType() ?: 'mixed'))."
*/";
}
/**
* Adds the function declaration for a default accessor.
*
* @param string &$script
* @param Column $column
*/
public function addDefaultAccessorOpen(&$script, Column $column)
{
$cfc = $column->getPhpName();
$visibility = $column->getAccessorVisibility();
$script .= "
".$visibility." function get$cfc(";
if ($column->isLazyLoad()) {
$script .= "ConnectionInterface \$con = null";
}
$script .= ")
{";
}
/**
* Adds the function body for a default accessor method.
*
* @param string &$script
* @param Column $column
*/
protected function addDefaultAccessorBody(&$script, Column $column)
{
$clo = $column->getLowercasedName();
if ($column->isLazyLoad()) {
$script .= $this->getAccessorLazyLoadSnippet($column);
}
$script .= "
return \$this->$clo;";
}
/**
* Adds the function close for a default accessor method.
*
* @param string &$script
*/
protected function addDefaultAccessorClose(&$script)
{
$script .= "
}
";
}
/**
* Adds the lazy loader method.
*
* @param string &$script
* @param Column $column
*/
protected function addLazyLoader(&$script, Column $column)
{
$this->addLazyLoaderComment($script, $column);
$this->addLazyLoaderOpen($script, $column);
$this->addLazyLoaderBody($script, $column);
$this->addLazyLoaderClose($script);
}
/**
* Adds the comment for the lazy loader method.
*
* @param string &$script
* @param Column $column
*/
protected function addLazyLoaderComment(&$script, Column $column)
{
$clo = $column->getLowercasedName();
$script .= "
/**
* Load the value for the lazy-loaded [$clo] column.
*
* This method performs an additional query to return the value for
* the [$clo] column, since it is not populated by
* the hydrate() method.
*
* @param \$con ConnectionInterface (optional) The ConnectionInterface connection to use.
* @return void
* @throws PropelException - any underlying error will be wrapped and re-thrown.
*/";
}
/**
* Adds the function declaration for the lazy loader method.
*
* @param string &$script
* @param Column $column
*/
protected function addLazyLoaderOpen(&$script, Column $column)
{
$cfc = $column->getPhpName();
$script .= "
protected function load$cfc(ConnectionInterface \$con = null)
{";
}
/**
* 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 .= "
\$c = \$this->buildPkeyCriteria();
\$c->addSelectColumn(".$this->getColumnConstant($column).");
try {
\$row = array(0 => null);
\$dataFetcher = ".$this->getQueryClassName()."::create(null, \$c)->setFormatter(ModelCriteria::FORMAT_STATEMENT)->find(\$con);
if (\$dataFetcher instanceof PDODataFetcher) {
\$dataFetcher->bindColumn(1, \$row[0], PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
}
\$row = \$dataFetcher->fetch(PDO::FETCH_BOUND);
\$dataFetcher->close();";
} else {
$script .= "
\$c = \$this->buildPkeyCriteria();
\$c->addSelectColumn(".$this->getColumnConstant($column).");
try {
\$dataFetcher = ".$this->getQueryClassName()."::create(null, \$c)->setFormatter(ModelCriteria::FORMAT_STATEMENT)->find(\$con);
\$row = \$dataFetcher->fetch();
\$dataFetcher->close();";
}
$script .= "
\$firstColumn = \$row ? current(\$row) : null;
";
if ($column->getType() === PropelTypes::CLOB && $platform instanceof OraclePlatform) {
// PDO_OCI returns a stream for CLOB objects, while other PDO adapters return a string...
$script .= "
if (\$firstColumn) {
\$this->$clo = stream_get_contents(\$firstColumn);
}";
} elseif ($column->isLobType() && !$platform->hasStreamBlobImpl()) {
$script .= "
if (\$firstColumn !== null) {
\$this->$clo = fopen('php://memory', 'r+');
fwrite(\$this->$clo, \$firstColumn);
rewind(\$this->$clo);
} else {
\$this->$clo = null;
}";
} elseif ($column->isPhpPrimitiveType()) {
$script .= "
\$this->$clo = (\$firstColumn !== null) ? (".$column->getPhpType().") \$firstColumn : null;";
} elseif ($column->isPhpObjectType()) {
$script .= "
\$this->$clo = (\$firstColumn !== null) ? new ".$column->getPhpType()."(\$firstColumn) : null;";
} else {
$script .= "
\$this->$clo = \$firstColumn;";
}
$script .= "
\$this->".$clo."_isLoaded = true;
} catch (Exception \$e) {
throw new PropelException(\"Error loading value for [$clo] column on demand.\", 0, \$e);
}";
}
/**
* Adds the function close for the lazy loader.
*
* @param string &$script
*/
protected function addLazyLoaderClose(&$script)
{
$script .= "
}";
}
/**
* Adds the open of the mutator (setter) method for a column.
*
* @param string &$script
* @param Column $column
*/
protected function addMutatorOpen(&$script, Column $column)
{
$this->addMutatorComment($script, $column);
$this->addMutatorOpenOpen($script, $column);
$this->addMutatorOpenBody($script, $column);
}
/**
* Adds the open of the mutator (setter) method for a JSON column.
*
* @param string &$script
* @param Column $column
*/
protected function addJsonMutatorOpen(&$script, Column $column)
{
$this->addJsonMutatorComment($script, $column);
$this->addMutatorOpenOpen($script, $column);
$this->addMutatorOpenBody($script, $column);
}
/**
* Adds the comment for a mutator.
*
* @param string &$script
* @param Column $column
*/
public function addJsonMutatorComment(&$script, Column $column)
{
$clo = $column->getLowercasedName();
$script .= "
/**
* Set the value of [$clo] column.
* ".$column->getDescription()."
* @param string|array|object \$v new value
* @return \$this|".$this->getObjectClassName(true)." The current object (for fluent API support)
*/";
}
/**
* Adds the comment for a mutator.
*
* @param string &$script
* @param Column $column
*/
public function addMutatorComment(&$script, Column $column)
{
$clo = $column->getLowercasedName();
$script .= "
/**
* Set the value of [$clo] column.
* ".$column->getDescription()."
* @param ".($column->getPhpType() ?: 'mixed')." \$v new value
* @return \$this|".$this->getObjectClassName(true)." The current object (for fluent API support)
*/";
}
/**
* Adds the mutator function declaration.
*
* @param string &$script
* @param Column $column
*/
public function addMutatorOpenOpen(&$script, Column $column)
{
$cfc = $column->getPhpName();
$visibility = $this->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';
}
}
$script .= "
".$visibility." function set$cfc($typeHint\$v$null)
{";
}
/**
* Adds the mutator open body part.
*
* @param string &$script
* @param Column $column
*/
protected function addMutatorOpenBody(&$script, Column $column)
{
$clo = $column->getLowercasedName();
$cfc = $column->getPhpName();
if ($column->isLazyLoad()) {
$script .= "
// explicitly set the is-loaded flag to true for this lazy load col;
// it doesn't matter if the value is actually set or not (logic below) as
// any attempt to set the value means that no db lookup should be performed
// when the get$cfc() method is called.
\$this->".$clo."_isLoaded = true;
";
}
}
/**
* Adds the close of the mutator (setter) method for a column.
*
* @param string &$script
* @param Column $column
*/
protected function addMutatorClose(&$script, Column $column)
{
$this->addMutatorCloseBody($script, $column);
$this->addMutatorCloseClose($script, $column);
}
/**
* Adds the body of the close part of a mutator.
*
* @param string &$script
* @param Column $column
*/
protected function addMutatorCloseBody(&$script, Column $column)
{
$table = $this->getTable();
if ($column->isForeignKey()) {
foreach ($column->getForeignKeys() as $fk) {
$tblFK = $table->getDatabase()->getTable($fk->getForeignTableName());
$colFK = $tblFK->getColumn($fk->getMappedForeignColumn($column->getName()));
if (!$colFK) {
continue;
}
$varName = $this->getFKVarName($fk);
$script .= "
if (\$this->$varName !== null && \$this->".$varName."->get".$colFK->getPhpName()."() !== \$v) {
\$this->$varName = null;
}
";
} // foreach fk
} /* if col is foreign key */
foreach ($column->getReferrers() as $refFK) {
$tblFK = $this->getDatabase()->getTable($refFK->getForeignTableName());
if ( $tblFK->getName() != $table->getName() ) {
foreach ($column->getForeignKeys() as $fk) {
$tblFK = $table->getDatabase()->getTable($fk->getForeignTableName());
$colFK = $tblFK->getColumn($fk->getMappedForeignColumn($column->getName()));
if ($refFK->isLocalPrimaryKey()) {
$varName = $this->getPKRefFKVarName($refFK);
$script .= "
// update associated ".$tblFK->getPhpName()."
if (\$this->$varName !== null) {
\$this->{$varName}->set".$colFK->getPhpName()."(\$v);
}
";
} else {
$collName = $this->getRefFKCollVarName($refFK);
$script .= "
// update associated ".$tblFK->getPhpName()."
if (\$this->$collName !== null) {
foreach (\$this->$collName as \$referrerObject) {
\$referrerObject->set".$colFK->getPhpName()."(\$v);
}
}
";
} // if (isLocalPrimaryKey
} // foreach col->getPrimaryKeys()
} // if tablFk != table
} // foreach
}
/**
* Adds the close for the mutator close
* @param string &$script The script will be modified in this method.
* @param Column $col The current column.
* @see addMutatorClose()
**/
protected function addMutatorCloseClose(&$script, Column $col)
{
$cfc = $col->getPhpName();
$script .= "
return \$this;
} // set$cfc()
";
}
/**
* Adds a setter for BLOB columns.
* @param string &$script The script will be modified in this method.
* @param Column $col The current column.
* @see parent::addColumnMutators()
*/
protected function addLobMutator(&$script, Column $col)
{
$this->addMutatorOpen($script, $col);
$clo = $col->getLowercasedName();
$script .= "
// Because BLOB columns are streams in PDO we have to assume that they are
// always modified when a new value is passed in. For example, the contents
// of the stream itself may have changed externally.
if (!is_resource(\$v) && \$v !== null) {
\$this->$clo = fopen('php://memory', 'r+');
fwrite(\$this->$clo, \$v);
rewind(\$this->$clo);
} else { // it's already a stream
\$this->$clo = \$v;
}
\$this->modifiedColumns[".$this->getColumnConstant($col)."] = true;
";
$this->addMutatorClose($script, $col);
} // addLobMutatorSnippet
/**
* Adds a setter method for date/time/timestamp columns.
* @param string &$script The script will be modified in this method.
* @param Column $col The current column.
* @see parent::addColumnMutators()
*/
protected function addTemporalMutator(&$script, Column $col)
{
$clo = $col->getLowercasedName();
$dateTimeClass = $this->getDateTimeClass($col);
$this->declareClasses($dateTimeClass, '\Propel\Runtime\Util\PropelDateTime');
$this->addTemporalMutatorComment($script, $col);
$this->addMutatorOpenOpen($script, $col);
$this->addMutatorOpenBody($script, $col);
$fmt = var_export($this->getTemporalFormatter($col), true);
$script .= "
\$dt = PropelDateTime::newInstance(\$v, null, '$dateTimeClass');
if (\$this->$clo !== null || \$dt !== null) {";
if (($def = $col->getDefaultValue()) !== null && !$def->isExpression()) {
$defaultValue = $this->getDefaultValueString($col);
$script .= "
if ( (\$dt != \$this->{$clo}) // normalized values don't match
|| (\$dt->format($fmt) === $defaultValue) // or the entered value matches the default
) {";
} else {
switch ($col->getType()) {
case 'DATE':
$format = 'Y-m-d';
break;
case 'TIME':
$format = 'H:i:s.u';
break;
default:
$format = 'Y-m-d H:i:s.u';
}
$script .= "
if (\$this->{$clo} === null || \$dt === null || \$dt->format(\"$format\") !== \$this->{$clo}->format(\"$format\")) {";
}
$script .= "
\$this->$clo = \$dt === null ? null : clone \$dt;
\$this->modifiedColumns[".$this->getColumnConstant($col)."] = true;
}
} // if either are not null
";
$this->addMutatorClose($script, $col);
}
public function addTemporalMutatorComment(&$script, Column $col)
{
$clo = $col->getLowercasedName();
$script .= "
/**
* Sets the value of [$clo] column to a normalized version of the date/time value specified.
* ".$col->getDescription()."
* @param mixed \$v string, integer (timestamp), or \DateTimeInterface value.
* Empty strings are treated as NULL.
* @return \$this|".$this->getObjectClassName(true)." The current object (for fluent API support)
*/";
}
/**
* Adds a setter for Object columns.
* @param string &$script The script will be modified in this method.
* @param Column $col The current column.
* @see parent::addColumnMutators()
*/
protected function addObjectMutator(&$script, Column $col)
{
$clo = $col->getLowercasedName();
$cloUnserialized = $clo.'_unserialized';
$this->addMutatorOpen($script, $col);
$script .= "
if (null === \$this->$clo || stream_get_contents(\$this->$clo) !== serialize(\$v)) {
\$this->$cloUnserialized = \$v;
\$this->$clo = fopen('php://memory', 'r+');
fwrite(\$this->$clo, serialize(\$v));
\$this->modifiedColumns[".$this->getColumnConstant($col)."] = true;
}
rewind(\$this->$clo);
";
$this->addMutatorClose($script, $col);
}
/**
* Adds a setter for Json columns.
* @param string &$script The script will be modified in this method.
* @param Column $col The current column.
* @see parent::addColumnMutators()
*/
protected function addJsonMutator(&$script, Column $col)
{
$clo = $col->getLowercasedName();
$this->addJsonMutatorOpen($script, $col);
$script .= "
if (is_string(\$v)) {
// JSON as string needs to be decoded/encoded to get a reliable comparison (spaces, ...)
\$v = json_decode(\$v);
}
\$encodedValue = json_encode(\$v);
if (\$encodedValue !== \$this->$clo) {
\$this->$clo = \$encodedValue;
\$this->modifiedColumns[".$this->getColumnConstant($col)."] = true;
}
";
$this->addMutatorClose($script, $col);
}
/**
* Adds a setter for Array columns.
* @param string &$script The script will be modified in this method.
* @param Column $col The current column.
* @see parent::addColumnMutators()
*/
protected function addArrayMutator(&$script, Column $col)
{
$clo = $col->getLowercasedName();
$cloUnserialized = $clo.'_unserialized';
$this->addMutatorOpen($script, $col);
$script .= "
if (\$this->$cloUnserialized !== \$v) {
\$this->$cloUnserialized = \$v;
\$this->$clo = '| ' . implode(' | ', \$v) . ' |';
\$this->modifiedColumns[".$this->getColumnConstant($col)."] = true;
}
";
$this->addMutatorClose($script, $col);
}
/**
* Adds a push method for an array column.
* @param string &$script The script will be modified in this method.
* @param Column $col The current column.
*/
protected function addAddArrayElement(&$script, Column $col)
{
$clo = $col->getLowercasedName();
$cfc = $col->getPhpName();
$visibility = $col->getAccessorVisibility();
$singularPhpName = $col->getPhpSingularName();
$columnType = ($col->getType() === PropelTypes::PHP_ARRAY) ? 'array' : 'set';
$script .= "
/**
* Adds a value to the [$clo] $columnType column value.
* @param mixed \$value
* ".$col->getDescription();
if ($col->isLazyLoad()) {
$script .= "
* @param ConnectionInterface \$con An optional ConnectionInterface connection to use for fetching this lazy-loaded column.";
}
$script .= "
* @return \$this|".$this->getObjectClassName(true)." The current object (for fluent API support)
*/
$visibility function add$singularPhpName(\$value";
if ($col->isLazyLoad()) {
$script .= ", ConnectionInterface \$con = null";
}
$script .= ")
{
\$currentArray = \$this->get$cfc(";
if ($col->isLazyLoad()) {
$script .= "\$con";
}
$script .= ");
\$currentArray []= \$value;
\$this->set$cfc(\$currentArray);
return \$this;
} // add$singularPhpName()
";
}
/**
* 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 .= "
/**
* Removes a value from the [$clo] $columnType column value.
* @param mixed \$value
* ".$col->getDescription();
if ($col->isLazyLoad()) {
$script .= "
* @param ConnectionInterface \$con An optional ConnectionInterface connection to use for fetching this lazy-loaded column.";
}
$script .= "
* @return \$this|".$this->getObjectClassName(true)." The current object (for fluent API support)
*/
$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 .= ")
{
\$targetArray = array();
foreach (\$this->get$cfc(";
if ($col->isLazyLoad()) {
$script .= "\$con";
}
$script .= ") as \$element) {
if (\$element != \$value) {
\$targetArray []= \$element;
}
}
\$this->set$cfc(\$targetArray);
return \$this;
} // remove$singularPhpName()
";
}
/**
* Adds a setter for Enum columns.
* @param string &$script The script will be modified in this method.
* @param Column $col The current column.
* @see parent::addColumnMutators()
*/
protected function addEnumMutator(&$script, Column $col)
{
$clo = $col->getLowercasedName();
$this->addEnumMutatorComment($script, $col);
$this->addMutatorOpenOpen($script, $col);
$this->addMutatorOpenBody($script, $col);
$script .= "
if (\$v !== null) {
\$valueSet = " . $this->getTableMapClassName() . "::getValueSet(" . $this->getColumnConstant($col) . ");
if (!in_array(\$v, \$valueSet)) {
throw new PropelException(sprintf('Value \"%s\" is not accepted in this enumerated column', \$v));
}
\$v = array_search(\$v, \$valueSet);
}
if (\$this->$clo !== \$v) {
\$this->$clo = \$v;
\$this->modifiedColumns[".$this->getColumnConstant($col)."] = true;
}
";
$this->addMutatorClose($script, $col);
}
/**
* Adds the comment for an enum mutator.
*
* @param string &$script
* @param Column $column
*/
public function addEnumMutatorComment(&$script, Column $column)
{
$clo = $column->getLowercasedName();
$script .= "
/**
* Set the value of [$clo] column.
* ".$column->getDescription()."
* @param string \$v new value
* @return \$this|".$this->getObjectClassName(true)." The current object (for fluent API support)
* @throws \\Propel\\Runtime\\Exception\\PropelException
*/";
}
/**
* Adds a setter for SET column mutator.
*
* @param string &$script The script will be modified in this method.
* @param Column $col The current column.
* @see parent::addColumnMutators()
*/
protected function addSetMutator(&$script, Column $col)
{
$clo = $col->getLowercasedName();
$this->addSetMutatorComment($script, $col);
$this->addMutatorOpenOpen($script, $col);
$this->addMutatorOpenBody($script, $col);
$cloConverted = $clo . '_converted';
$this->declareClasses(
'Propel\Common\Util\SetColumnConverter',
'Propel\Common\Exception\SetColumnConverterException'
);
$script .= "
if (\$this->$cloConverted === null || count(array_diff(\$this->$cloConverted, \$v)) > 0 || count(array_diff(\$v, \$this->$cloConverted)) > 0) {
\$valueSet = " . $this->getTableMapClassName() . "::getValueSet(" . $this->getColumnConstant($col) . ");
try {
\$v = SetColumnConverter::convertToInt(\$v, \$valueSet);
} catch (SetColumnConverterException \$e) {
throw new PropelException(sprintf('Value \"%s\" is not accepted in this set column', \$e->getValue()), \$e->getCode(), \$e);
}
if (\$this->$clo !== \$v) {
\$this->$cloConverted = null;
\$this->$clo = \$v;
\$this->modifiedColumns[".$this->getColumnConstant($col)."] = true;
}
}
";
$this->addMutatorClose($script, $col);
}
/**
* Adds the comment for a SET column mutator.
*
* @param string &$script
* @param Column $column
*/
public function addSetMutatorComment(&$script, Column $column)
{
$clo = $column->getLowercasedName();
$script .= "
/**
* Set the value of [$clo] column.
* ".$column->getDescription()."
* @param array \$v new value
* @return \$this|".$this->getObjectClassName(true)." The current object (for fluent API support)
* @throws \\Propel\\Runtime\\Exception\\PropelException
*/";
}
/**
* Adds setter method for boolean columns.
* @param string &$script The script will be modified in this method.
* @param Column $col The current column.
* @see parent::addColumnMutators()
*/
protected function addBooleanMutator(&$script, Column $col)
{
$clo = $col->getLowercasedName();
$this->addBooleanMutatorComment($script, $col);
$this->addMutatorOpenOpen($script, $col);
$this->addMutatorOpenBody($script, $col);
$script .= "
if (\$v !== null) {
if (is_string(\$v)) {
\$v = in_array(strtolower(\$v), array('false', 'off', '-', 'no', 'n', '0', '')) ? false : true;
} else {
\$v = (boolean) \$v;
}
}
if (\$this->$clo !== \$v) {
\$this->$clo = \$v;
\$this->modifiedColumns[".$this->getColumnConstant($col)."] = true;
}
";
$this->addMutatorClose($script, $col);
}
public function addBooleanMutatorComment(&$script, Column $col)
{
$clo = $col->getLowercasedName();
$script .= "
/**
* Sets the value of the [$clo] column.
* Non-boolean arguments are converted using the following rules:
* * 1, '1', 'true', 'on', and 'yes' are converted to boolean true
* * 0, '0', 'false', 'off', and 'no' are converted to boolean false
* Check on string values is case insensitive (so 'FaLsE' is seen as 'false').
* ".$col->getDescription()."
* @param boolean|integer|string \$v The new value
* @return \$this|".$this->getObjectClassName(true)." The current object (for fluent API support)
*/";
}
/**
* Adds setter method for "normal" columns.
* @param string &$script The script will be modified in this method.
* @param Column $col The current column.
* @see parent::addColumnMutators()
*/
protected function addDefaultMutator(&$script, Column $col)
{
$clo = $col->getLowercasedName();
$this->addMutatorOpen($script, $col);
// Perform type-casting to ensure that we can use type-sensitive
// checking in mutators.
if ($col->isPhpPrimitiveType()) {
$script .= "
if (\$v !== null) {
\$v = (".$col->getPhpType().") \$v;
}
";
}
$script .= "
if (\$this->$clo !== \$v) {
\$this->$clo = \$v;
\$this->modifiedColumns[".$this->getColumnConstant($col)."] = true;
}
";
$this->addMutatorClose($script, $col);
}
/**
* Adds the hasOnlyDefaultValues() method.
* @param string &$script The script will be modified in this method.
*/
protected function addHasOnlyDefaultValues(&$script)
{
$this->addHasOnlyDefaultValuesComment($script);
$this->addHasOnlyDefaultValuesOpen($script);
$this->addHasOnlyDefaultValuesBody($script);
$this->addHasOnlyDefaultValuesClose($script);
}
/**
* Adds the comment for the hasOnlyDefaultValues method
* @param string &$script The script will be modified in this method.
* @see addHasOnlyDefaultValues
**/
protected function addHasOnlyDefaultValuesComment(&$script)
{
$script .= "
/**
* Indicates whether the columns in this object are only set to default values.
*
* This method can be used in conjunction with isModified() to indicate whether an object is both
* modified _and_ has some values set which are non-default.
*
* @return boolean Whether the columns in this object are only been set with default values.
*/";
}
/**
* Adds the function declaration for the hasOnlyDefaultValues method
* @param string &$script The script will be modified in this method.
* @see addHasOnlyDefaultValues
**/
protected function addHasOnlyDefaultValuesOpen(&$script)
{
$script .= "
public function hasOnlyDefaultValues()
{";
}
/**
* Adds the function body for the hasOnlyDefaultValues method
* @param string &$script The script will be modified in this method.
* @see addHasOnlyDefaultValues
**/
protected function addHasOnlyDefaultValuesBody(&$script)
{
$table = $this->getTable();
$colsWithDefaults = [];
foreach ($table->getColumns() as $col) {
$def = $col->getDefaultValue();
if ($def !== null && !$def->isExpression()) {
$colsWithDefaults[] = $col;
}
}
foreach ($colsWithDefaults as $col) {
/** @var Column $col */
$clo = $col->getLowercasedName();
$accessor = "\$this->$clo";
if ($col->isTemporalType()) {
$fmt = $this->getTemporalFormatter($col);
$accessor = "\$this->$clo && \$this->{$clo}->format('$fmt')";
}
$script .= "
if ($accessor !== " . $this->getDefaultValueString($col).") {
return false;
}
";
}
}
/**
* Adds the function close for the hasOnlyDefaultValues method
* @param string &$script The script will be modified in this method.
* @see addHasOnlyDefaultValues
**/
protected function addHasOnlyDefaultValuesClose(&$script)
{
$script .= "
// otherwise, everything was equal, so return TRUE
return true;";
$script .= "
} // hasOnlyDefaultValues()
";
}
/**
* Adds the hydrate() method, which sets attributes of the object based on a ResultSet.
* @param string &$script The script will be modified in this method.
*/
protected function addHydrate(&$script)
{
$this->addHydrateComment($script);
$this->addHydrateOpen($script);
$this->addHydrateBody($script);
$this->addHydrateClose($script);
}
/**
* Adds the comment for the hydrate method
* @param string &$script The script will be modified in this method.
* @see addHydrate()
*/
protected function addHydrateComment(&$script)
{
$script .= "
/**
* Hydrates (populates) the object variables with values from the database resultset.
*
* An offset (0-based \"start column\") is specified so that objects can be hydrated
* with a subset of the columns in the resultset rows. This is needed, for example,
* for results of JOIN queries where the resultset row includes columns from two or
* more tables.
*
* @param array \$row The row returned by DataFetcher->fetch().
* @param int \$startcol 0-based offset column which indicates which restultset column to start with.
* @param boolean \$rehydrate Whether this object is being re-hydrated from the database.
* @param string \$indexType The index type of \$row. Mostly DataFetcher->getIndexType().
One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME
* TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM.
*
* @return int next starting column
* @throws PropelException - Any caught Exception will be rewrapped as a PropelException.
*/";
}
/**
* Adds the function declaration for the hydrate method
* @param string &$script The script will be modified in this method.
* @see addHydrate()
*/
protected function addHydrateOpen(&$script)
{
$script .= "
public function hydrate(\$row, \$startcol = 0, \$rehydrate = false, \$indexType = TableMap::TYPE_NUM)
{";
}
/**
* Adds the function body for the hydrate method
* @param string &$script The script will be modified in this method.
* @see addHydrate()
*/
protected function addHydrateBody(&$script)
{
$table = $this->getTable();
$platform = $this->getPlatform();
$tableMap = $this->getTableMapClassName();
$script .= "
try {";
$n = 0;
foreach ($table->getColumns() as $col) {
if (!$col->isLazyLoad()) {
$indexName = "TableMap::TYPE_NUM == \$indexType ? $n + \$startcol : $tableMap::translateFieldName('{$col->getPhpName()}', TableMap::TYPE_PHPNAME, \$indexType)";
$script .= "
\$col = \$row[$indexName];";
$clo = $col->getLowercasedName();
if ($col->getType() === PropelTypes::CLOB_EMU && $this->getPlatform() instanceof OraclePlatform) {
// PDO_OCI returns a stream for CLOB objects, while other PDO adapters return a string...
$script .= "
\$this->$clo = stream_get_contents(\$col);";
} elseif ($col->isLobType() && !$platform->hasStreamBlobImpl()) {
$script .= "
if (null !== \$col) {
\$this->$clo = fopen('php://memory', 'r+');
fwrite(\$this->$clo, \$col);
rewind(\$this->$clo);
} else {
\$this->$clo = null;
}";
} elseif ($col->isTemporalType()) {
$dateTimeClass = $this->getDateTimeClass($col);
$handleMysqlDate = false;
if ($this->getPlatform() instanceof MysqlPlatform) {
if ($col->getType() === PropelTypes::TIMESTAMP) {
$handleMysqlDate = true;
$mysqlInvalidDateString = '0000-00-00 00:00:00';
} elseif ($col->getType() === PropelTypes::DATE) {
$handleMysqlDate = true;
$mysqlInvalidDateString = '0000-00-00';
}
// 00:00:00 is a valid time, so no need to check for that.
}
if ($handleMysqlDate) {
$script .= "
if (\$col === '$mysqlInvalidDateString') {
\$col = null;
}";
}
$script .= "
\$this->$clo = (null !== \$col) ? PropelDateTime::newInstance(\$col, null, '$dateTimeClass') : null;";
} elseif ($col->isPhpPrimitiveType()) {
$script .= "
\$this->$clo = (null !== \$col) ? (".$col->getPhpType().") \$col : null;";
} elseif ($col->getType() === PropelTypes::OBJECT) {
$script .= "
\$this->$clo = \$col;";
} elseif ($col->getType() === PropelTypes::PHP_ARRAY) {
$cloUnserialized = $clo . '_unserialized';
$script .= "
\$this->$clo = \$col;
\$this->$cloUnserialized = null;";
} elseif ($col->isSetType()) {
$cloConverted = $clo . '_converted';
$script .= "
\$this->$clo = \$col;
\$this->$cloConverted = null;";
} elseif ($col->isPhpObjectType()) {
$script .= "
\$this->$clo = (null !== \$col) ? new ".$col->getPhpType()."(\$col) : null;";
} else {
$script .= "
\$this->$clo = \$col;";
}
$n++;
} // if col->isLazyLoad()
} /* foreach */
if ($this->getBuildProperty("generator.objectModel.addSaveMethod")) {
$script .= "
\$this->resetModified();
";
}
$script .= "
\$this->setNew(false);
if (\$rehydrate) {
\$this->ensureConsistency();
}
";
$this->applyBehaviorModifier('postHydrate', $script, " ");
$script .= "
return \$startcol + $n; // $n = ".$this->getTableMapClass()."::NUM_HYDRATE_COLUMNS.
} catch (Exception \$e) {
throw new PropelException(sprintf('Error populating %s object', ".var_export($this->getStubObjectBuilder()->getClassName(), true)."), 0, \$e);
}";
}
/**
* Adds the function close for the hydrate method
* @param string &$script The script will be modified in this method.
* @see addHydrate()
*/
protected function addHydrateClose(&$script)
{
$script .= "
}
";
}
/**
* Adds the buildPkeyCriteria method
* @param string &$script The script will be modified in this method.
**/
protected function addBuildPkeyCriteria(&$script)
{
$this->declareClass('Propel\\Runtime\\Exception\\LogicException');
$this->addBuildPkeyCriteriaComment($script);
$this->addBuildPkeyCriteriaOpen($script);
$this->addBuildPkeyCriteriaBody($script);
$this->addBuildPkeyCriteriaClose($script);
}
/**
* Adds the comment for the buildPkeyCriteria method
* @param string &$script The script will be modified in this method.
* @see addBuildPkeyCriteria()
**/
protected function addBuildPkeyCriteriaComment(&$script)
{
$script .= "
/**
* Builds a Criteria object containing the primary key for this object.
*
* Unlike buildCriteria() this method includes the primary key values regardless
* of whether or not they have been modified.
*
* @throws LogicException if no primary key is defined
*
* @return Criteria The Criteria object containing value(s) for primary key(s).
*/";
}
/**
* Adds the function declaration for the buildPkeyCriteria method
* @param string &$script The script will be modified in this method.
* @see addBuildPkeyCriteria()
**/
protected function addBuildPkeyCriteriaOpen(&$script)
{
$script .= "
public function buildPkeyCriteria()
{";
}
/**
* Adds the function body for the buildPkeyCriteria method
* @param string &$script The script will be modified in this method.
* @see addBuildPkeyCriteria()
**/
protected function addBuildPkeyCriteriaBody(&$script)
{
if (!$this->getTable()->getPrimaryKey()) {
$script .= "
throw new LogicException('The {$this->getObjectName()} object has no primary key');";
return;
}
$script .= "
\$criteria = ".$this->getQueryClassName()."::create();";
foreach ($this->getTable()->getPrimaryKey() as $col) {
$clo = $col->getLowercasedName();
$script .= "
\$criteria->add(".$this->getColumnConstant($col).", \$this->$clo);";
}
}
/**
* Adds the function close for the buildPkeyCriteria method
* @param string &$script The script will be modified in this method.
* @see addBuildPkeyCriteria()
**/
protected function addBuildPkeyCriteriaClose(&$script)
{
$script .= "
return \$criteria;
}
";
}
/**
* Adds the buildCriteria method
* @param string &$script The script will be modified in this method.
**/
protected function addBuildCriteria(&$script)
{
$this->addBuildCriteriaComment($script);
$this->addBuildCriteriaOpen($script);
$this->addBuildCriteriaBody($script);
$this->addBuildCriteriaClose($script);
}
/**
* Adds comment for the buildCriteria method
* @param string &$script The script will be modified in this method.
* @see addBuildCriteria()
**/
protected function addBuildCriteriaComment(&$script)
{
$script .= "
/**
* Build a Criteria object containing the values of all modified columns in this object.
*
* @return Criteria The Criteria object containing all modified values.
*/";
}
/**
* Adds the function declaration of the buildCriteria method
* @param string &$script The script will be modified in this method.
* @see addBuildCriteria()
**/
protected function addBuildCriteriaOpen(&$script)
{
$script .= "
public function buildCriteria()
{";
}
/**
* Adds the function body of the buildCriteria method
* @param string &$script The script will be modified in this method.
* @see addBuildCriteria()
**/
protected function addBuildCriteriaBody(&$script)
{
$script .= "
\$criteria = new Criteria(".$this->getTableMapClass()."::DATABASE_NAME);
";
foreach ($this->getTable()->getColumns() as $col) {
$clo = $col->getLowercasedName();
$script .= "
if (\$this->isColumnModified(".$this->getColumnConstant($col).")) {
\$criteria->add(".$this->getColumnConstant($col).", \$this->$clo);
}";
}
}
/**
* Adds the function close of the buildCriteria method
* @param string &$script The script will be modified in this method.
* @see addBuildCriteria()
**/
protected function addBuildCriteriaClose(&$script)
{
$script .= "
return \$criteria;
}
";
}
/**
* Adds the toArray method
* @param string &$script The script will be modified in this method.
**/
protected function addToArray(&$script)
{
$fks = $this->getTable()->getForeignKeys();
$referrers = $this->getTable()->getReferrers();
$hasFks = count($fks) > 0 || count($referrers) > 0;
$objectClassName = $this->getUnqualifiedClassName();
$pkGetter = $this->getTable()->hasCompositePrimaryKey() ? 'serialize($this->getPrimaryKey())' : '$this->getPrimaryKey()';
$defaultKeyType = $this->getDefaultKeyType();
$script .= "
/**
* Exports the object as an array.
*
* You can specify the key type of the array by passing one of the class
* type constants.
*
* @param string \$keyType (optional) One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME,
* TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM.
* Defaults to TableMap::$defaultKeyType.
* @param boolean \$includeLazyLoadColumns (optional) Whether to include lazy loaded columns. Defaults to TRUE.
* @param array \$alreadyDumpedObjects List of objects to skip to avoid recursion";
if ($hasFks) {
$script .= "
* @param boolean \$includeForeignObjects (optional) Whether to include hydrated related objects. Default to FALSE.";
}
$script .= "
*
* @return array an associative array containing the field names (as keys) and field values
*/
public function toArray(\$keyType = TableMap::$defaultKeyType, \$includeLazyLoadColumns = true, \$alreadyDumpedObjects = array()" . ($hasFks ? ", \$includeForeignObjects = false" : '') . ")
{
if (isset(\$alreadyDumpedObjects['$objectClassName'][\$this->hashCode()])) {
return '*RECURSION*';
}
\$alreadyDumpedObjects['$objectClassName'][\$this->hashCode()] = true;
\$keys = ".$this->getTableMapClassName()."::getFieldNames(\$keyType);
\$result = array(";
foreach ($this->getTable()->getColumns() as $num => $col) {
if ($col->isLazyLoad()) {
$script .= "
\$keys[$num] => (\$includeLazyLoadColumns) ? \$this->get".$col->getPhpName()."() : null,";
} else {
$script .= "
\$keys[$num] => \$this->get".$col->getPhpName()."(),";
}
}
$script .= "
);";
foreach ($this->getTable()->getColumns() as $num => $col) {
if ($col->isTemporalType()) {
$script .= "
if (\$result[\$keys[$num]] instanceof \DateTimeInterface) {
\$result[\$keys[$num]] = \$result[\$keys[$num]]->format('c');
}
";
}
}
$script .= "
\$virtualColumns = \$this->virtualColumns;
foreach (\$virtualColumns as \$key => \$virtualColumn) {
\$result[\$key] = \$virtualColumn;
}
";
if ($hasFks) {
$script .= "
if (\$includeForeignObjects) {";
foreach ($fks as $fk) {
$script .= "
if (null !== \$this->" . $this->getFKVarName($fk) . ") {
{$this->addToArrayKeyLookUp($fk->getPhpName(), $fk->getForeignTable(), false)}
\$result[\$key] = \$this->" . $this->getFKVarName($fk) . "->toArray(\$keyType, \$includeLazyLoadColumns, \$alreadyDumpedObjects, true);
}";
}
foreach ($referrers as $fk) {
if ($fk->isLocalPrimaryKey()) {
$script .= "
if (null !== \$this->" . $this->getPKRefFKVarName($fk) . ") {
{$this->addToArrayKeyLookUp($fk->getRefPhpName(), $fk->getTable(), false)}
\$result[\$key] = \$this->" . $this->getPKRefFKVarName($fk) . "->toArray(\$keyType, \$includeLazyLoadColumns, \$alreadyDumpedObjects, true);
}";
} else {
$script .= "
if (null !== \$this->" . $this->getRefFKCollVarName($fk) . ") {
{$this->addToArrayKeyLookUp($fk->getRefPhpName(), $fk->getTable(), true)}
\$result[\$key] = \$this->" . $this->getRefFKCollVarName($fk) . "->toArray(null, false, \$keyType, \$includeLazyLoadColumns, \$alreadyDumpedObjects);
}";
}
}
$script .= "
}";
}
$script .= "
return \$result;
}
";
} // addToArray()
/**
* Adds the switch-statement for looking up the array-key name for toArray
* @see toArray
*/
protected function addToArrayKeyLookUp($phpName, Table $table, $plural)
{
if($phpName == "") {
$phpName = $table->getPhpName();
}
$camelCaseName = $table->getCamelCaseName();
$fieldName = $table->getName();
if ($plural) {
$phpName = $this->getPluralizer()->getPluralForm($phpName);
$camelCaseName = $this->getPluralizer()->getPluralForm($camelCaseName);
$fieldName = $this->getPluralizer()->getPluralForm($fieldName);
}
return "
switch (\$keyType) {
case TableMap::TYPE_CAMELNAME:
\$key = '" . $camelCaseName . "';
break;
case TableMap::TYPE_FIELDNAME:
\$key = '" . $fieldName . "';
break;
default:
\$key = '" . $phpName . "';
}
";
}
/**
* Adds the getByName method
* @param string &$script The script will be modified in this method.
**/
protected function addGetByName(&$script)
{
$this->addGetByNameComment($script);
$this->addGetByNameOpen($script);
$this->addGetByNameBody($script);
$this->addGetByNameClose($script);
}
/**
* Adds the comment for the getByName method
* @param string &$script The script will be modified in this method.
* @see addGetByName
**/
protected function addGetByNameComment(&$script)
{
$defaultKeyType = $this->getDefaultKeyType();
$script .= "
/**
* Retrieves a field from the object by name passed in as a string.
*
* @param string \$name name
* @param string \$type The type of fieldname the \$name is of:
* one of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME
* TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM.
* Defaults to TableMap::$defaultKeyType.
* @return mixed Value of field.
*/";
}
/**
* Adds the function declaration for the getByName method
* @param string &$script The script will be modified in this method.
* @see addGetByName
**/
protected function addGetByNameOpen(&$script)
{
$defaultKeyType = $this->getDefaultKeyType();
$script .= "
public function getByName(\$name, \$type = TableMap::$defaultKeyType)
{";
}
/**
* Adds the function body for the getByName method
* @param string &$script The script will be modified in this method.
* @see addGetByName
**/
protected function addGetByNameBody(&$script)
{
$script .= "
\$pos = ".$this->getTableMapClassName()."::translateFieldName(\$name, \$type, TableMap::TYPE_NUM);
\$field = \$this->getByPosition(\$pos);";
}
/**
* Adds the function close for the getByName method
* @param string &$script The script will be modified in this method.
* @see addGetByName
**/
protected function addGetByNameClose(&$script)
{
$script .= "
return \$field;
}
";
}
/**
* Adds the getByPosition method
* @param string &$script The script will be modified in this method.
**/
protected function addGetByPosition(&$script)
{
$this->addGetByPositionComment($script);
$this->addGetByPositionOpen($script);
$this->addGetByPositionBody($script);
$this->addGetByPositionClose($script);
}
/**
* Adds comment for the getByPosition method
* @param string &$script The script will be modified in this method.
* @see addGetByPosition
**/
protected function addGetByPositionComment(&$script)
{
$script .= "
/**
* Retrieves a field from the object by Position as specified in the xml schema.
* Zero-based.
*
* @param int \$pos position in xml schema
* @return mixed Value of field at \$pos
*/";
}
/**
* Adds the function declaration for the getByPosition method
* @param string &$script The script will be modified in this method.
* @see addGetByPosition
**/
protected function addGetByPositionOpen(&$script)
{
$script .= "
public function getByPosition(\$pos)
{";
}
/**
* Adds the function body for the getByPosition method
* @param string &$script The script will be modified in this method.
* @see addGetByPosition
**/
protected function addGetByPositionBody(&$script)
{
$table = $this->getTable();
$script .= "
switch (\$pos) {";
$i = 0;
foreach ($table->getColumns() as $col) {
$cfc = $col->getPhpName();
$script .= "
case $i:
return \$this->get$cfc();
break;";
$i++;
} /* foreach */
$script .= "
default:
return null;
break;
} // switch()";
}
/**
* Adds the function close for the getByPosition method
* @param string &$script The script will be modified in this method.
* @see addGetByPosition
**/
protected function addGetByPositionClose(&$script)
{
$script .= "
}
";
}
protected function addSetByName(&$script)
{
$defaultKeyType = $this->getDefaultKeyType();
$script .= "
/**
* Sets a field from the object by name passed in as a string.
*
* @param string \$name
* @param mixed \$value field value
* @param string \$type The type of fieldname the \$name is of:
* one of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME
* TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM.
* Defaults to TableMap::$defaultKeyType.
* @return \$this|".$this->getObjectClassName(true)."
*/
public function setByName(\$name, \$value, \$type = TableMap::$defaultKeyType)
{
\$pos = ".$this->getTableMapClassName()."::translateFieldName(\$name, \$type, TableMap::TYPE_NUM);
return \$this->setByPosition(\$pos, \$value);
}
";
}
protected function addSetByPosition(&$script)
{
$table = $this->getTable();
$script .= "
/**
* Sets a field from the object by Position as specified in the xml schema.
* Zero-based.
*
* @param int \$pos position in xml schema
* @param mixed \$value field value
* @return \$this|".$this->getObjectClassName(true)."
*/
public function setByPosition(\$pos, \$value)
{
switch (\$pos) {";
$i = 0;
foreach ($table->getColumns() as $col) {
$cfc = $col->getPhpName();
$script .= "
case $i:";
if (PropelTypes::ENUM === $col->getType()) {
$script .= "
\$valueSet = " . $this->getTableMapClassName() . "::getValueSet(" . $this->getColumnConstant($col) . ");
if (isset(\$valueSet[\$value])) {
\$value = \$valueSet[\$value];
}";
} elseif ($col->isSetType()) {
$this->declareClasses(
'Propel\Common\Util\SetColumnConverter',
'Propel\Common\Exception\SetColumnConverterException'
);
$script .= "
\$valueSet = " . $this->getTableMapClassName() . "::getValueSet(" . $this->getColumnConstant($col) . ");
try {
\$value = SetColumnConverter::convertIntToArray(\$value, \$valueSet);
} catch (SetColumnConverterException \$e) {
throw new PropelException('Unknown stored set key: ' . \$e->getValue(), \$e->getCode(), \$e);
}
";
} elseif (PropelTypes::PHP_ARRAY === $col->getType()) {
$script .= "
if (!is_array(\$value)) {
\$v = trim(substr(\$value, 2, -2));
\$value = \$v ? explode(' | ', \$v) : array();
}";
}
$script .= "
\$this->set$cfc(\$value);
break;";
$i++;
} /* foreach */
$script .= "
} // switch()
return \$this;
}
";
}
protected function addFromArray(&$script)
{
$defaultKeyType = $this->getDefaultKeyType();
$table = $this->getTable();
$script .= "
/**
* Populates the object using an array.
*
* This is particularly useful when populating an object from one of the
* request arrays (e.g. \$_POST). This method goes through the column
* names, checking to see whether a matching key exists in populated
* array. If so the setByName() method is called for that column.
*
* You can specify the key type of the array by additionally passing one
* of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME,
* TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM.
* The default key type is the column's TableMap::$defaultKeyType.
*
* @param array \$arr An array to populate the object from.
* @param string \$keyType The type of keys the array uses.
* @return void
*/
public function fromArray(\$arr, \$keyType = TableMap::$defaultKeyType)
{
\$keys = ".$this->getTableMapClassName()."::getFieldNames(\$keyType);
";
foreach ($table->getColumns() as $num => $col) {
$cfc = $col->getPhpName();
$script .= "
if (array_key_exists(\$keys[$num], \$arr)) {
\$this->set$cfc(\$arr[\$keys[$num]]);
}";
} /* foreach */
$script .= "
}
";
}
protected function addImportFrom(&$script)
{
$defaultKeyType = $this->getDefaultKeyType();
$script .= "
/**
* Populate the current object from a string, using a given parser format
* <code>
* \$book = new Book();
* \$book->importFrom('JSON', '{\"Id\":9012,\"Title\":\"Don Juan\",\"ISBN\":\"0140422161\",\"Price\":12.99,\"PublisherId\":1234,\"AuthorId\":5678}');
* </code>
*
* You can specify the key type of the array by additionally passing one
* of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME,
* TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM.
* The default key type is the column's TableMap::$defaultKeyType.
*
* @param mixed \$parser A AbstractParser instance,
* or a format name ('XML', 'YAML', 'JSON', 'CSV')
* @param string \$data The source data to import from
* @param string \$keyType The type of keys the array uses.
*
* @return \$this|".$this->getObjectClassName(true)." The current object, for fluid interface
*/
public function importFrom(\$parser, \$data, \$keyType = TableMap::$defaultKeyType)
{
if (!\$parser instanceof AbstractParser) {
\$parser = AbstractParser::getParser(\$parser);
}
\$this->fromArray(\$parser->toArray(\$data), \$keyType);
return \$this;
}
";
}
/**
* Adds a delete() method to remove the object form the datastore.
* @param string &$script The script will be modified in this method.
*/
protected function addDelete(&$script)
{
$this->addDeleteComment($script);
$this->addDeleteOpen($script);
$this->addDeleteBody($script);
$this->addDeleteClose($script);
}
/**
* Adds the comment for the delete function
* @param string &$script The script will be modified in this method.
* @see addDelete()
**/
protected function addDeleteComment(&$script)
{
$className = $this->getUnqualifiedClassName();
$script .= "
/**
* Removes this object from datastore and sets delete attribute.
*
* @param ConnectionInterface \$con
* @return void
* @throws PropelException
* @see $className::setDeleted()
* @see $className::isDeleted()
*/";
}
/**
* Adds the function declaration for the delete function
* @param string &$script The script will be modified in this method.
* @see addDelete()
**/
protected function addDeleteOpen(&$script)
{
$script .= "
public function delete(ConnectionInterface \$con = null)
{";
}
/**
* Adds the function body for the delete function
* @param string &$script The script will be modified in this method.
* @see addDelete()
**/
protected function addDeleteBody(&$script)
{
$script .= "
if (\$this->isDeleted()) {
throw new PropelException(\"This object has already been deleted.\");
}
if (\$con === null) {
\$con = Propel::getServiceContainer()->getWriteConnection(".$this->getTableMapClass()."::DATABASE_NAME);
}
\$con->transaction(function () use (\$con) {
\$deleteQuery = ".$this->getQueryClassName()."::create()
->filterByPrimaryKey(\$this->getPrimaryKey());";
if ($this->getBuildProperty('generator.objectModel.addHooks')) {
$script .= "
\$ret = \$this->preDelete(\$con);";
// apply behaviors
$this->applyBehaviorModifier('preDelete', $script, " ");
$script .= "
if (\$ret) {
\$deleteQuery->delete(\$con);
\$this->postDelete(\$con);";
// apply behaviors
$this->applyBehaviorModifier('postDelete', $script, " ");
$script .= "
\$this->setDeleted(true);
}";
} else {
// apply behaviors
$this->applyBehaviorModifier('preDelete', $script, " ");
$script .= "
\$deleteQuery->delete(\$con);";
// apply behaviors
$this->applyBehaviorModifier('postDelete