/** * @param ObjectBuilder $builder * @param string $script * @param Column $column */ protected function appendAddVersionMethod(ObjectBuilder $builder, &$script, $column) { $columnPhpName = $column->getPhpName(); $methodName = "add{$columnPhpName}Version"; $logTable = $this->getLogTable($column); $logTableName = $logTable->getName(); $logARClassName = $builder->getClassNameFromBuilder($builder->getNewStubObjectBuilder($logTable)); $logARQueryName = $builder->getNewStubQueryBuilder($logTable)->getFullyQualifiedClassName(); $varName = lcfirst($column->getPhpName()); $script .= "\n/**\n * @return {$logARClassName}|false model instance of saved log ({$logTableName}) or false if nothing was changed.\n */\npublic function {$methodName}()\n{\n if (!isset(\$this->changeLoggerTracker['{$varName}'])) {\n return false;\n }\n\n \$log = new {$logARClassName}();"; foreach ($this->getTable()->getPrimaryKey() as $col) { $script .= "\n \$log->set" . $col->getPhpName() . "(\$this->get" . $col->getPhpName() . "());"; } $script .= "\n \$log->set" . $column->getPhpName() . "(\$this->changeLoggerTracker['{$varName}']);"; if ('true' === $this->getParameter('created_at')) { $createdAtColumn = $logTable->getColumn($this->getParameter('created_at_column')); $script .= "\n \$log->set{$createdAtColumn->getPhpName()}(time());\n"; } if ('true' === $this->getParameter('created_by')) { $createdByColumn = $logTable->getColumn($this->getParameter('created_by_column')); $methodGetName = 'get' . $column->getPhpName() . 'ChangeBy'; $script .= "\n \$log->set{$createdByColumn->getPhpName()}(\$this->{$methodGetName}());\n"; } if ('true' === $this->getParameter('comment')) { $commentColumn = $logTable->getColumn($this->getParameter('comment_column')); $methodGetName = 'get' . $column->getPhpName() . 'ChangeComment'; $script .= "\n \$log->set{$commentColumn->getPhpName()}(\$this->{$methodGetName}());\n"; } $script .= "\n \$lastVersion = {$logARQueryName}::create()\n ->filterByOrigin(\$this)\n ->orderByVersion('desc')\n ->findOne();\n\n \$log->setVersion(\$lastVersion ? \$lastVersion->getVersion() + 1 : 1);\n \$log->save();\n\n \$this->changeLoggerTracker['{$varName}'] = \$this->get{$column->getPhpName()}();\n return \$log;\n}\n"; }
/** * Adds the singular filterByCol method for an Array column. * @param string &$script The script will be modified in this method. * @param Column $col */ protected function addFilterByArrayCol(&$script, Column $col) { $colPhpName = $col->getPhpName(); $singularPhpName = $col->getPhpSingularName(); $colName = $col->getName(); $variableName = $col->getCamelCaseName(); $qualifiedName = $this->getColumnConstant($col); $script .= "\n /**\n * Filter the query on the {$colName} column\n * @param mixed \${$variableName} The value to use as filter\n * @param string \$comparison Operator to use for the column comparison, defaults to Criteria::CONTAINS_ALL\n *\n * @return \$this|" . $this->getQueryClassName() . " The current query, for fluid interface\n */\n public function filterBy{$singularPhpName}(\${$variableName} = null, \$comparison = null)\n {\n if (null === \$comparison || \$comparison == Criteria::CONTAINS_ALL) {\n if (is_scalar(\${$variableName})) {\n \${$variableName} = '%| ' . \${$variableName} . ' |%';\n \$comparison = Criteria::LIKE;\n }\n } elseif (\$comparison == Criteria::CONTAINS_NONE) {\n \${$variableName} = '%| ' . \${$variableName} . ' |%';\n \$comparison = Criteria::NOT_LIKE;\n \$key = \$this->getAliasedColName({$qualifiedName});\n if (\$this->containsKey(\$key)) {\n \$this->addAnd(\$key, \${$variableName}, \$comparison);\n } else {\n \$this->addAnd(\$key, \${$variableName}, \$comparison);\n }\n \$this->addOr(\$key, null, Criteria::ISNULL);\n\n return \$this;\n }\n\n return \$this->addUsingAlias({$qualifiedName}, \${$variableName}, \$comparison);\n }\n"; }
/** * Adds a remove method for an array column. * @param string &$script The script will be modified in this method. * @param Column $col The current column. */ protected function addRemoveArrayElement(&$script, Column $col) { $clo = $col->getLowercasedName(); $cfc = $col->getPhpName(); $visibility = $col->getAccessorVisibility(); $singularPhpName = $col->getPhpSingularName(); $columnType = $col->getType() === PropelTypes::PHP_ARRAY ? 'array' : 'set'; $script .= "\n /**\n * Removes a value from the [{$clo}] {$columnType} column value.\n * @param mixed \$value\n * " . $col->getDescription(); if ($col->isLazyLoad()) { $script .= "\n * @param ConnectionInterface \$con An optional ConnectionInterface connection to use for fetching this lazy-loaded column."; } $script .= "\n * @return \$this|" . $this->getObjectClassName(true) . " The current object (for fluent API support)\n */\n {$visibility} function remove{$singularPhpName}(\$value"; if ($col->isLazyLoad()) { $script .= ", ConnectionInterface \$con = null"; } // we want to reindex the array, so array_ functions are not the best choice $script .= ")\n {\n \$targetArray = array();\n foreach (\$this->get{$cfc}("; if ($col->isLazyLoad()) { $script .= "\$con"; } $script .= ") as \$element) {\n if (\$element != \$value) {\n \$targetArray []= \$element;\n }\n }\n \$this->set{$cfc}(\$targetArray);\n\n return \$this;\n } // remove{$singularPhpName}()\n"; }
/** * Adds a new column to the table. * * @param Column|array $col * @throws EngineException * @return Column */ public function addColumn($col) { if ($col instanceof Column) { if (isset($this->columnsByName[$col->getName()])) { throw new EngineException(sprintf('Column "%s" declared twice in table "%s"', $col->getName(), $this->getName())); } $col->setTable($this); if ($col->isInheritance()) { $this->inheritanceColumn = $col; } $this->columns[] = $col; $this->columnsByName[$col->getName()] = $col; $this->columnsByLowercaseName[strtolower($col->getName())] = $col; $this->columnsByPhpName[$col->getPhpName()] = $col; $col->setPosition(count($this->columns)); if ($col->requiresTransactionInPostgres()) { $this->needsTransactionInPostgres = true; } return $col; } $column = new Column(); $column->setTable($this); $column->loadMapping($col); return $this->addColumn($column); // call self w/ different param }
/** * Adds the singular filterByCol method for an Array column. * * @param string &$script The script will be modified in this method. * @param Column $col */ protected function addFilterBySetCol(&$script, Column $col) { $colPhpName = $col->getPhpName(); $singularPhpName = $col->getPhpSingularName(); $colName = $col->getName(); $variableName = $col->getCamelCaseName(); $script .= "\n /**\n * Filter the query on the {$colName} column\n * @param mixed \${$variableName} The value to use as filter\n * @param string \$comparison Operator to use for the column comparison, defaults to Criteria::CONTAINS_ALL\n *\n * @return \$this|" . $this->getQueryClassName() . " The current query, for fluid interface\n */\n public function filterBy{$singularPhpName}(\${$variableName} = null, \$comparison = null)\n {\n return \$this->filterBy{$colPhpName}(\${$variableName}, \$comparison);\n }\n"; }
public function addBooleanMutatorComment(&$script, Column $col) { $cfc = $col->getPhpName(); $clo = strtolower($col->getName()); $script .= "\n /**\n * Sets the value of the [{$clo}] column.\n * Non-boolean arguments are converted using the following rules:\n * * 1, '1', 'true', 'on', and 'yes' are converted to boolean true\n * * 0, '0', 'false', 'off', and 'no' are converted to boolean false\n * Check on string values is case insensitive (so 'FaLsE' is seen as 'false').\n * " . $col->getDescription() . "\n * @param boolean|integer|string \$v The new value\n * @return " . $this->getObjectClassname() . " The current object (for fluent API support)\n */"; }
/** * Appends the generated <column> XML node to its parent node. * * @param Column $column The Column model instance * @param \DOMNode $parentNode The parent DOMNode object */ private function appendColumnNode(Column $column, \DOMNode $parentNode) { $columnNode = $parentNode->appendChild($this->document->createElement('column')); $columnNode->setAttribute('name', $column->getName()); if ($phpName = $column->getPhpName()) { $columnNode->setAttribute('phpName', $phpName); } $columnNode->setAttribute('type', $column->getType()); $domain = $column->getDomain(); if ($size = $domain->getSize()) { $columnNode->setAttribute('size', $size); } if (null !== ($scale = $domain->getScale())) { $columnNode->setAttribute('scale', $scale); } $platform = $column->getPlatform(); if ($platform && !$column->isDefaultSqlType($platform)) { $columnNode->setAttribute('sqlType', $domain->getSqlType()); } if ($description = $column->getDescription()) { $columnNode->setAttribute('description', $description); } if ($column->isPrimaryKey()) { $columnNode->setAttribute('primaryKey', 'true'); } if ($column->isAutoIncrement()) { $columnNode->setAttribute('autoIncrement', 'true'); } if ($column->isNotNull()) { $columnNode->setAttribute('required', 'true'); } $defaultValue = $domain->getDefaultValue(); if ($defaultValue) { $type = $defaultValue->isExpression() ? 'defaultExpr' : 'defaultValue'; $columnNode->setAttribute($type, $defaultValue->getValue()); } if ($column->isInheritance()) { $columnNode->setAttribute('inheritance', $column->getInheritanceType()); foreach ($column->getInheritanceList() as $inheritance) { $this->appendInheritanceNode($inheritance, $columnNode); } } if ($column->isNodeKey()) { $columnNode->setAttribute('nodeKey', 'true'); if ($nodeKeySeparator = $column->getNodeKeySep()) { $columnNode->setAttribute('nodeKeySep', $nodeKeySeparator); } } foreach ($column->getVendorInformation() as $vendorInformation) { $this->appendVendorInformationNode($vendorInformation, $columnNode); } }
public function testPhpSingularName() { $column = new Column(); $column->setPhpName('Aliases'); $this->assertEquals($column->getPhpName(), 'Aliases'); $this->assertEquals($column->getPhpSingularName(), 'Aliase'); $column = new Column(); $column->setPhpName('Aliases'); $column->setPhpSingularName('Alias'); $this->assertEquals($column->getPhpName(), 'Aliases'); $this->assertEquals($column->getPhpSingularName(), 'Alias'); }
public function testSetCustomPhpName() { $column = new Column('created_at'); $column->setPhpName('CreatedAt'); $this->assertSame('CreatedAt', $column->getPhpName()); $this->assertSame('createdAt', $column->getStudlyPhpName()); }
/** * Adds the filterByCol method for this object. * * @param string &$script The script will be modified in this method. * @param \Propel\Generator\Model\Column $col * * @return void */ protected function addFilterByCol(&$script, Column $col) { $allowedArrayFilters = $this->getAllowedArrayFilters(); $implodedArrayComparisons = implode(', ', $allowedArrayFilters); $this->declareClass('Spryker\\Zed\\Propel\\Business\\Exception\\AmbiguousComparisonException'); $this->declareClass('Spryker\\Zed\\Propel\\Business\\Runtime\\ActiveQuery\\Criteria', 'Spryker'); $colPhpName = $col->getPhpName(); $colName = $col->getName(); $variableName = $col->getCamelCaseName(); $qualifiedName = $this->getColumnConstant($col); $script .= $this->addFilterByColBetween($col); $script .= $this->addFilterByColIn($col); $script .= $this->addFilterByColLike($col); $script .= "\n /**\n * Filter the query on the {$colName} column\n *"; if ($col->isNumericType()) { $script .= "\n * Example usage:\n * <code>\n * \$query->filterBy{$colPhpName}(1234); // WHERE {$colName} = 1234\n * \$query->filterBy{$colPhpName}(array(12, 34), Criteria::IN); // WHERE {$colName} IN (12, 34)\n * \$query->filterBy{$colPhpName}(array('min' => 12), SprykerCriteria::BETWEEN); // WHERE {$colName} > 12\n * </code>"; if ($col->isForeignKey()) { foreach ($col->getForeignKeys() as $fk) { $script .= "\n *\n * @see filterBy" . $this->getFKPhpNameAffix($fk) . "()"; } } $script .= "\n *\n * @param mixed \${$variableName} The value to use as filter.\n * Use scalar values for equality.\n * Use array values for in_array() equivalent. Add Criteria::IN explicitly.\n * Use associative array('min' => \$minValue, 'max' => \$maxValue) for intervals. Add SprykerCriteria::BETWEEN explicitly."; } elseif ($col->isTemporalType()) { $script .= "\n * Example usage:\n * <code>\n * \$query->filterBy{$colPhpName}('2011-03-14'); // WHERE {$colName} = '2011-03-14'\n * \$query->filterBy{$colPhpName}('now'); // WHERE {$colName} = '2011-03-14'\n * \$query->filterBy{$colPhpName}(array('max' => 'yesterday'), SprykerCriteria::BETWEEN); // WHERE {$colName} > '2011-03-13'\n * </code>\n *\n * @param mixed \${$variableName} The value to use as filter.\n * Values can be integers (unix timestamps), DateTime objects, or strings.\n * Empty strings are treated as NULL.\n * Use scalar values for equality.\n * Use array values for in_array() equivalent. Add Criteria::IN explicitly.\n * Use associative array('min' => \$minValue, 'max' => \$maxValue) for intervals. Add SprykerCriteria::BETWEEN explicitly."; } elseif ($col->getType() == PropelTypes::PHP_ARRAY) { $script .= "\n * @param array \${$variableName} The values to use as filter. Use Criteria::LIKE to enable like matching of array values."; } elseif ($col->isTextType()) { $script .= "\n * Example usage:\n * <code>\n * \$query->filterBy{$colPhpName}('fooValue'); // WHERE {$colName} = 'fooValue'\n * \$query->filterBy{$colPhpName}('%fooValue%', Criteria::LIKE); // WHERE {$colName} LIKE '%fooValue%'\n * </code>\n *\n * @param string \${$variableName} The value to use as filter.\n * Accepts wildcards (* and % trigger a LIKE). Add Criteria::LIKE explicitly."; } elseif ($col->isBooleanType()) { $script .= "\n * Example usage:\n * <code>\n * \$query->filterBy{$colPhpName}(true); // WHERE {$colName} = true\n * \$query->filterBy{$colPhpName}('yes'); // WHERE {$colName} = true\n * </code>\n *\n * @param boolean|string \${$variableName} The value to use as filter.\n * Non-boolean arguments are converted using the following rules:\n * * 1, '1', 'true', 'on', and 'yes' are converted to boolean true\n * * 0, '0', 'false', 'off', and 'no' are converted to boolean false\n * Check on string values is case insensitive (so 'FaLsE' is seen as 'false')."; } else { $script .= "\n * @param mixed \${$variableName} The value to use as filter"; } $script .= "\n * @param string \$comparison Operator to use for the column comparison, defaults to Criteria::EQUAL\n *\n * @return \$this|" . $this->getQueryClassName() . " The current query, for fluid interface\n *\n * @throws \\Spryker\\Zed\\Propel\\Business\\Exception\\AmbiguousComparisonException\n */\n public function filterBy{$colPhpName}(\${$variableName} = null, \$comparison = Criteria::EQUAL)\n {"; if ($col->isNumericType() || $col->isTemporalType()) { $script .= "\n\n if (is_array(\${$variableName})) {\n \$useMinMax = false;\n if (isset(\${$variableName}['min'])) {\n if (\$comparison != SprykerCriteria::BETWEEN && \$comparison != Criteria::GREATER_EQUAL) {\n throw new AmbiguousComparisonException('\\'min\\' requires explicit Criteria::GREATER_EQUAL or SprykerCriteria::BETWEEN when \\'max\\' is also needed as comparison criteria.');\n }\n \$this->addUsingAlias({$qualifiedName}, \${$variableName}['min'], Criteria::GREATER_EQUAL);\n \$useMinMax = true;\n }\n if (isset(\${$variableName}['max'])) {\n if (\$comparison != SprykerCriteria::BETWEEN && \$comparison != Criteria::LESS_EQUAL) {\n throw new AmbiguousComparisonException('\\'max\\' requires explicit Criteria::LESS_EQUAL or SprykerCriteria::BETWEEN when \\'min\\' is also needed as comparison criteria.');\n }\n \$this->addUsingAlias({$qualifiedName}, \${$variableName}['max'], Criteria::LESS_EQUAL);\n \$useMinMax = true;\n }\n if (\$useMinMax) {\n return \$this;\n }\n\n if (!in_array(\$comparison, [{$implodedArrayComparisons}])) {\n throw new AmbiguousComparisonException('\${$variableName} of type array requires one of [{$implodedArrayComparisons}] as comparison criteria.');\n }\n }"; } elseif ($col->getType() == PropelTypes::OBJECT) { $script .= "\n if (is_object(\${$variableName})) {\n \${$variableName} = serialize(\${$variableName});\n }"; } elseif ($col->getType() == PropelTypes::PHP_ARRAY) { $script .= "\n \$key = \$this->getAliasedColName({$qualifiedName});\n if (null === \$comparison || \$comparison == Criteria::CONTAINS_ALL) {\n foreach (\${$variableName} as \$value) {\n \$value = '%| ' . \$value . ' |%';\n if (\$this->containsKey(\$key)) {\n \$this->addAnd(\$key, \$value, Criteria::LIKE);\n } else {\n \$this->add(\$key, \$value, Criteria::LIKE);\n }\n }\n\n return \$this;\n } elseif (\$comparison == Criteria::CONTAINS_SOME) {\n foreach (\${$variableName} as \$value) {\n \$value = '%| ' . \$value . ' |%';\n if (\$this->containsKey(\$key)) {\n \$this->addOr(\$key, \$value, Criteria::LIKE);\n } else {\n \$this->add(\$key, \$value, Criteria::LIKE);\n }\n }\n\n return \$this;\n } elseif (\$comparison == Criteria::CONTAINS_NONE) {\n foreach (\${$variableName} as \$value) {\n \$value = '%| ' . \$value . ' |%';\n if (\$this->containsKey(\$key)) {\n \$this->addAnd(\$key, \$value, Criteria::NOT_LIKE);\n } else {\n \$this->add(\$key, \$value, Criteria::NOT_LIKE);\n }\n }\n \$this->addOr(\$key, null, Criteria::ISNULL);\n\n return \$this;\n }"; } elseif ($col->getType() == PropelTypes::ENUM) { $script .= "\n \$valueSet = " . $this->getTableMapClassName() . "::getValueSet(" . $this->getColumnConstant($col) . ");\n if (is_scalar(\${$variableName})) {\n if (!in_array(\${$variableName}, \$valueSet)) {\n throw new PropelException(sprintf('Value \"%s\" is not accepted in this enumerated column', \${$variableName}));\n }\n \${$variableName} = array_search(\${$variableName}, \$valueSet);\n } elseif (is_array(\${$variableName})) {\n if (!in_array(\$comparison, [{$implodedArrayComparisons}])) {\n throw new AmbiguousComparisonException('array requires one of [{$implodedArrayComparisons}] as comparison criteria.');\n }\n \$convertedValues = array();\n foreach (\${$variableName} as \$value) {\n if (!in_array(\$value, \$valueSet)) {\n throw new PropelException(sprintf('Value \"%s\" is not accepted in this enumerated column', \$value));\n }\n \$convertedValues []= array_search(\$value, \$valueSet);\n }\n \${$variableName} = \$convertedValues;\n }"; } elseif ($col->isTextType()) { $script .= "\n if (\$comparison == Criteria::LIKE) {\n \${$variableName} = str_replace('*', '%', \${$variableName});\n }\n\n if (is_array(\${$variableName}) && !in_array(\$comparison, [{$implodedArrayComparisons}])) {\n throw new AmbiguousComparisonException('\${$variableName} of type array requires one of [{$implodedArrayComparisons}] as comparison criteria.');\n }"; } elseif ($col->isBooleanType()) { $script .= "\n if (is_string(\${$variableName})) {\n \${$variableName} = in_array(strtolower(\${$variableName}), array('false', 'off', '-', 'no', 'n', '0', '')) ? false : true;\n }"; } $script .= "\n\n return \$this->addUsingAlias({$qualifiedName}, \${$variableName}, \$comparison);\n }\n"; }
protected function addFindOne(Column $column) { return $this->renderTemplate('queryFindOne', ['objectClassName' => $this->builder->getClassNameFromBuilder($this->builder->getStubObjectBuilder($column->getTable())), 'columnPhpName' => $column->getPhpName(), 'columnName' => $column->getName(), 'localeColumnName' => $this->behavior->getLocaleColumn()->getPhpName()]); }