示例#1
0
 /**
  * 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)
 {
     $cfc = $col->getPhpName();
     $clo = strtolower($col->getName());
     $visibility = $col->getMutatorVisibility();
     $dateTimeClass = $this->getBuildProperty('dateTimeClass');
     if (!$dateTimeClass) {
         $dateTimeClass = 'DateTime';
     }
     $script .= "\n\t/**\n\t * Sets the value of [{$clo}] column to a normalized version of the date/time value specified.\n\t * " . $col->getDescription() . "\n\t * @param      mixed \$v string, integer (timestamp), or DateTime value.  Empty string will\n\t *\t\t\t\t\t\tbe treated as NULL for temporal objects.\n\t * @return     " . $this->getObjectClassname() . " The current object (for fluent API support)\n\t */\n\t" . $visibility . " function set{$cfc}(\$v)\n\t{";
     $this->addMutatorOpenBody($script, $col);
     $fmt = var_export($this->getTemporalFormatter($col), true);
     $script .= "\n\t\t// we treat '' as NULL for temporal objects because DateTime('') == DateTime('now')\n\t\t// -- which is unexpected, to say the least.\n\t\tif (\$v === null || \$v === '') {\n\t\t\t\$dt = null;\n\t\t} elseif (\$v instanceof DateTime) {\n\t\t\t\$dt = \$v;\n\t\t} else {\n\t\t\t// some string/numeric value passed; we normalize that so that we can\n\t\t\t// validate it.\n\t\t\ttry {\n\t\t\t\tif (is_numeric(\$v)) { // if it's a unix timestamp\n\t\t\t\t\t\$dt = new {$dateTimeClass}('@'.\$v, new DateTimeZone('UTC'));\n\t\t\t\t\t// We have to explicitly specify and then change the time zone because of a\n\t\t\t\t\t// DateTime bug: http://bugs.php.net/bug.php?id=43003\n\t\t\t\t\t\$dt->setTimeZone(new DateTimeZone(date_default_timezone_get()));\n\t\t\t\t} else {\n\t\t\t\t\t\$dt = new {$dateTimeClass}(\$v);\n\t\t\t\t}\n\t\t\t} catch (Exception \$x) {\n\t\t\t\tthrow new PropelException('Error parsing date/time value: ' . var_export(\$v, true), \$x);\n\t\t\t}\n\t\t}\n\n\t\tif ( \$this->{$clo} !== null || \$dt !== null ) {\n\t\t\t// (nested ifs are a little easier to read in this case)\n\n\t\t\t\$currNorm = (\$this->{$clo} !== null && \$tmpDt = new {$dateTimeClass}(\$this->{$clo})) ? \$tmpDt->format({$fmt}) : null;\n\t\t\t\$newNorm = (\$dt !== null) ? \$dt->format({$fmt}) : null;\n\n\t\t\tif ( (\$currNorm !== \$newNorm) // normalized values don't match ";
     if (($def = $col->getDefaultValue()) !== null && !$def->isExpression()) {
         $defaultValue = $this->getDefaultValueString($col);
         $script .= "\n\t\t\t\t\t|| (\$dt->format({$fmt}) === {$defaultValue}) // or the entered value matches the default";
     }
     $script .= "\n\t\t\t\t\t)\n\t\t\t{\n\t\t\t\t\$this->{$clo} = (\$dt ? \$dt->format({$fmt}) : null);\n\t\t\t\t\$this->modifiedColumns[] = " . $this->getColumnConstant($col) . ";\n\t\t\t}\n\t\t} // if either are not null\n";
     $this->addMutatorClose($script, $col);
 }
示例#2
0
 public function addBooleanMutatorComment(&$script, Column $col)
 {
     $cfc = $col->getPhpName();
     $clo = strtolower($col->getName());
     $script .= "\n\t/**\n\t * Sets the value of the [{$clo}] column. \n\t * Non-boolean arguments are converted using the following rules:\n\t *   * 1, '1', 'true',  'on',  and 'yes' are converted to boolean true\n\t *   * 0, '0', 'false', 'off', and 'no'  are converted to boolean false\n\t * Check on string values is case insensitive (so 'FaLsE' is seen as 'false').\n\t * " . $col->getDescription() . "\n\t * @param      boolean|integer|string \$v The new value\n\t * @return     " . $this->getObjectClassname() . " The current object (for fluent API support)\n\t */";
 }
 protected function getAddColumnComment(Column $column)
 {
     $pattern = "\nCOMMENT ON COLUMN %s.%s IS %s;\n";
     if ($description = $column->getDescription()) {
         return sprintf($pattern, $this->quoteIdentifier($column->getTable()->getName()), $this->quoteIdentifier($column->getName()), $this->quote($description));
     }
 }
 /**
  * Adds the comment for a mutator
  * @param string &$script The script will be modified in this method.
  * @param Column $col The current column.
  * @see        addMutatorOpen()
  **/
 public function addMutatorComment(&$script, Column $col)
 {
     $clo = strtolower($col->getName());
     $script .= "\n    /**\n     * Set the value of [{$clo}] column.\n     * " . $col->getDescription() . "\n     * @param " . $col->getPhpType() . "|null \$v new value\n     * @return " . $this->getObjectClassname() . " The current object (for fluent API support)\n     */";
 }
示例#5
0
 /**
  * Adds the open of the mutator (setter) method for a column.
  * @param      string &$script The script will be modified in this method.
  * @param      Column $col The current column.
  */
 protected function addMutatorOpen(&$script, Column $col)
 {
     $cfc = $col->getPhpName();
     $clo = strtolower($col->getName());
     $script .= "\n\t/**\n\t * Set the value of [{$clo}] column.\n\t * " . $col->getDescription() . "\n\t * @param      " . $col->getPhpNative() . " \$v new value\n\t * @return     void\n\t */\n\tpublic function set{$cfc}(\$v)\n\t{\n";
     if ($col->isLazyLoad()) {
         $script .= "\n\t\t// explicitly set the is-loaded flag to true for this lazy load col;\n\t\t// it doesn't matter if the value is actually set or not (logic below) as\n\t\t// any attempt to set the value means that no db lookup should be performed\n\t\t// when the get{$cfc}() method is called.\n\t\t\$this->" . $clo . "_isLoaded = true;\n";
     }
 }
	/**
	 * 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)
	{
		$cfc = $col->getPhpName();
		$clo = strtolower($col->getName());
		$visibility = $col->getMutatorVisibility();

		$dateTimeClass = $this->getBuildProperty('dateTimeClass');
		if (!$dateTimeClass) {
			$dateTimeClass = 'DateTime';
		}

		$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 DateTime value.  Empty string will
	 *						be treated as NULL for temporal objects.
	 * @return     ".$this->getObjectClassname()." The current object (for fluent API support)
	 */
	".$visibility." function set$cfc(\$v)
	{";
		if ($col->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;
";
		}

		$fmt = var_export($this->getTemporalFormatter($col), true);

		$script .= "
		// we treat '' as NULL for temporal objects because DateTime('') == DateTime('now')
		// -- which is unexpected, to say the least.
		if (\$v === null || \$v === '') {
			\$dt = null;
		} elseif (\$v instanceof DateTime) {
			\$dt = \$v;
		} else {
			// some string/numeric value passed; we normalize that so that we can
			// validate it.
			try {
				if (is_numeric(\$v)) { // if it's a unix timestamp
					\$dt = new $dateTimeClass('@'.\$v, new DateTimeZone('UTC'));
					// We have to explicitly specify and then change the time zone because of a
					// DateTime bug: http://bugs.php.net/bug.php?id=43003
					\$dt->setTimeZone(new DateTimeZone(date_default_timezone_get()));
				} else {
					\$dt = new $dateTimeClass(\$v);
				}
			} catch (Exception \$x) {
				throw new PropelException('Error parsing date/time value: ' . var_export(\$v, true), \$x);
			}
		}

		if ( \$this->$clo !== null || \$dt !== null ) {
			// (nested ifs are a little easier to read in this case)

			\$currNorm = (\$this->$clo !== null && \$tmpDt = new $dateTimeClass(\$this->$clo)) ? \$tmpDt->format($fmt) : null;
			\$newNorm = (\$dt !== null) ? \$dt->format($fmt) : null;

			if ( (\$currNorm !== \$newNorm) // normalized values don't match ";

		if (($def = $col->getDefaultValue()) !== null && !$def->isExpression()) {
			$defaultValue = $this->getDefaultValueString($col);
			$script .= "
					|| (\$dt->format($fmt) === $defaultValue) // or the entered value matches the default";
		}

		$script .= "
					)
			{
				\$this->$clo = (\$dt ? \$dt->format($fmt) : null);
				\$this->modifiedColumns[] = ".$this->getColumnConstant($col).";
			}
		} // if either are not null
";
		$this->addMutatorClose($script, $col);
	}
 /**
  * Builds the DDL SQL for a Column object.
  * @return     string
  */
 public function getColumnDDL(Column $col)
 {
     $platform = $this->getPlatform();
     $domain = $col->getDomain();
     $sqlType = $domain->getSqlType();
     $notNullString = $col->getNotNullString();
     $defaultSetting = $col->getDefaultSetting();
     // Special handling of TIMESTAMP/DATETIME types ...
     // See: http://propel.phpdb.org/trac/ticket/538
     if ($sqlType == 'DATETIME') {
         $def = $domain->getDefaultValue();
         if ($def && $def->isExpression()) {
             // DATETIME values can only have constant expressions
             $sqlType = 'TIMESTAMP';
         }
     } elseif ($sqlType == 'DATE') {
         $def = $domain->getDefaultValue();
         if ($def && $def->isExpression()) {
             throw new EngineException("DATE columns cannot have default *expressions* in MySQL.");
         }
     } elseif ($sqlType == 'TEXT' || $sqlType == 'BLOB') {
         if ($domain->getDefaultValue()) {
             throw new EngineException("BLOB and TEXT columns cannot have DEFAULT values. in MySQL.");
         }
     }
     $sb = "";
     $sb .= $this->quoteIdentifier($col->getName()) . " ";
     $sb .= $sqlType;
     if ($platform->hasSize($sqlType)) {
         $sb .= $domain->printSize();
     }
     $colinfo = $col->getVendorInfoForType($platform->getDatabaseType());
     if ($colinfo->hasParameter('Charset')) {
         $sb .= ' CHARACTER SET ' . $platform->quote($colinfo->getParameter('Charset'));
     }
     if ($colinfo->hasParameter('Collation')) {
         $sb .= ' COLLATE ' . $platform->quote($colinfo->getParameter('Collation'));
     } elseif ($colinfo->hasParameter('Collate')) {
         $sb .= ' COLLATE ' . $platform->quote($colinfo->getParameter('Collate'));
     }
     if ($sqlType == 'TIMESTAMP') {
         $notNullString = $col->getNotNullString();
         $defaultSetting = $col->getDefaultSetting();
         if ($notNullString == '') {
             $notNullString = 'NULL';
         }
         if ($defaultSetting == '' && $notNullString == 'NOT NULL') {
             $defaultSetting = 'DEFAULT CURRENT_TIMESTAMP';
         }
         $sb .= ' ' . $notNullString . ' ' . $defaultSetting;
     } else {
         $sb .= ' ' . $defaultSetting . ' ' . $notNullString;
     }
     if ($autoIncrement = $col->getAutoIncrementString()) {
         $sb .= ' ' . $autoIncrement;
     }
     if ($col->getDescription()) {
         $sb .= ' COMMENT ' . $platform->quote($col->getDescription());
     }
     return trim($sb);
 }