protected function addFilterByColBetween(Column $col) { $script = ''; if ($col->isNumericType() || $col->isTemporalType()) { $colPhpName = $col->getPhpName(); $variableName = $col->getCamelCaseName(); $queryClassName = $this->getQueryClassName(); $script .= <<<SCRIPT /** * Applies SprykerCriteria::BETWEEN filtering criteria for the column. * * @param array \${$variableName} Filter value. * [ * 'min' => 3, 'max' => 5 * ] * * 'min' and 'max' are optional, when neither is specified, throws \\Spryker\\Zed\\Propel\\Business\\Exception\\AmbiguousComparisonException. * * @return \$this|{$queryClassName} The current query, for fluid interface */ public function filterBy{$colPhpName}_Between(array \${$variableName}) { return \$this->filterBy{$colPhpName}(\${$variableName}, SprykerCriteria::BETWEEN); } SCRIPT; } return $script; }
/** * 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 .= "\n /**\n * The value for the {$clo} field.\n * " . $column->getDescription(); if ($column->getDefaultValue()) { if ($column->getDefaultValue()->isExpression()) { $script .= "\n * Note: this column has a database default value of: (expression) " . $column->getDefaultValue()->getValue(); } else { $script .= "\n * Note: this column has a database default value of: " . $this->getDefaultValueString($column); } } $script .= "\n * @var {$cptype}\n */"; }
/** * Get the PHP snippet for binding a value to a column. * Warning: duplicates logic from AdapterInterface::bindValue(). * Any code modification here must be ported there. */ public function getColumnBindingPHP(Column $column, $identifier, $columnValueAccessor, $tab = " ") { $script = ''; if ($column->isTemporalType()) { $columnValueAccessor = $columnValueAccessor . " ? " . $columnValueAccessor . "->format(\"" . $this->getTimeStampFormatter() . "\") : null"; } elseif ($column->isLobType()) { // we always need to make sure that the stream is rewound, otherwise nothing will // get written to database. $script .= "\nif (is_resource({$columnValueAccessor})) {\n rewind({$columnValueAccessor});\n}"; } $script .= sprintf("\n\$stmt->bindValue(%s, %s, %s);", $identifier, $columnValueAccessor, PropelTypes::getPdoTypeString($column->getType())); return preg_replace('/^(.+)/m', $tab . '$1', $script); }
/** * Returns the type-casted and stringified default value for the specified Column. * This only works for scalar default values currently. * @return string The default value or 'NULL' if there is none. */ protected function getDefaultValueString(Column $col) { $defaultValue = var_export(null, true); $val = $col->getPhpDefaultValue(); if ($val === null) { return $defaultValue; } if ($col->isTemporalType()) { $fmt = $this->getTemporalFormatter($col); 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 $x) { // 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"', $col->getDefaultValueString(), $col->getFullyQualifiedName()), $x); } } elseif ($col->isEnumType()) { $valueSet = $col->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 ($col->isPhpPrimitiveType()) { settype($val, $col->getPhpType()); $defaultValue = var_export($val, true); } elseif ($col->isPhpObjectType()) { $defaultValue = 'new ' . $col->getPhpType() . '(' . var_export($val, true) . ')'; } else { throw new EngineException("Cannot get default value string for " . $col->getFullyQualifiedName()); } return $defaultValue; }
/** * @dataProvider provideMappingTemporalTypes */ public function testTemporalType($mappingType) { $domain = $this->getDomainMock(); $domain->expects($this->once())->method('setType')->with($this->equalTo($mappingType)); $domain->expects($this->any())->method('getType')->will($this->returnValue($mappingType)); $column = new Column(); $column->setDomain($domain); $column->setType($mappingType); $this->assertSame('string', $column->getPhpType()); $this->assertTrue($column->isPhpPrimitiveType()); $this->assertTrue($column->isTemporalType()); }