Skip to content

Commit

Permalink
Merge pull request #1912 from mringler/bugfix/fix_findBy_signature
Browse files Browse the repository at this point in the history
fixed input type of findBy hints
  • Loading branch information
dereuromark authored Nov 15, 2022
2 parents 1c8062d + a1c385d commit cb84d9c
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 135 deletions.
181 changes: 46 additions & 135 deletions src/Propel/Generator/Builder/Om/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Propel\Generator\Builder\Om;

use Propel\Generator\Builder\Util\PropelTemplate;
use Propel\Generator\Model\Column;
use Propel\Generator\Model\CrossForeignKeys;
use Propel\Generator\Model\ForeignKey;
Expand Down Expand Up @@ -89,151 +90,61 @@ public function getParentClass(): string
protected function addClassOpen(string &$script): void
{
$table = $this->getTable();
$tableName = $table->getName();
$tableDesc = $table->getDescription();
$queryClass = $this->getQueryClassName();
$modelClass = $this->getObjectClassName();
$parentClass = $this->getParentClass();
$script .= "
/**
* Base class that represents a query for 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
*";
}

// magic orderBy() methods, for IDE completion
foreach ($this->getTable()->getColumns() as $column) {
$script .= "
* @method $queryClass orderBy" . $column->getPhpName() . '($order = Criteria::ASC) Order by the ' . $column->getName() . ' column';
}
$script .= "
*";

// magic groupBy() methods, for IDE completion
foreach ($this->getTable()->getColumns() as $column) {
$script .= "
* @method $queryClass groupBy" . $column->getPhpName() . '() Group by the ' . $column->getName() . ' column';
}

// override the signature of ModelCriteria::left-, right- and innerJoin to specify the class of the returned object, for IDE completion
$script .= "
*
* @method $queryClass leftJoin(\$relation) Adds a LEFT JOIN clause to the query
* @method $queryClass rightJoin(\$relation) Adds a RIGHT JOIN clause to the query
* @method $queryClass innerJoin(\$relation) Adds a INNER JOIN clause to the query
*";

$script .= "
* @method $queryClass leftJoinWith(\$relation) Adds a LEFT JOIN clause and with to the query
* @method $queryClass rightJoinWith(\$relation) Adds a RIGHT JOIN clause and with to the query
* @method $queryClass innerJoinWith(\$relation) Adds a INNER JOIN clause and with to the query
*";

$relationQueryClasses = [];

// magic XXXjoinYYY() methods, for IDE completion
foreach ($this->getTable()->getForeignKeys() as $fk) {
$relationName = $this->getFKPhpNameAffix($fk);

$script .= "
* @method $queryClass leftJoin" . $relationName . '($relationAlias = null) Adds a LEFT JOIN clause to the query using the ' . $relationName . " relation
* @method $queryClass rightJoin" . $relationName . '($relationAlias = null) Adds a RIGHT JOIN clause to the query using the ' . $relationName . " relation
* @method $queryClass innerJoin" . $relationName . '($relationAlias = null) Adds a INNER JOIN clause to the query using the ' . $relationName . " relation
*";

$script .= "
* @method $queryClass joinWith" . $relationName . '($joinType = Criteria::INNER_JOIN) Adds a join clause and with to the query using the ' . $relationName . " relation
*";

$script .= "
* @method $queryClass leftJoinWith" . $relationName . '() Adds a LEFT JOIN clause and with to the query using the ' . $relationName . " relation
* @method $queryClass rightJoinWith" . $relationName . '() Adds a RIGHT JOIN clause and with to the query using the ' . $relationName . " relation
* @method $queryClass innerJoinWith" . $relationName . '() Adds a INNER JOIN clause and with to the query using the ' . $relationName . " relation
*";

$relationQueryClasses[$this->getNewStubQueryBuilder($fk->getForeignTable())->getQueryClassName(true)] = true;
}
foreach ($this->getTable()->getReferrers() as $refFK) {
$relationName = $this->getRefFKPhpNameAffix($refFK);

$script .= "
* @method $queryClass leftJoin" . $relationName . '($relationAlias = null) Adds a LEFT JOIN clause to the query using the ' . $relationName . " relation
* @method $queryClass rightJoin" . $relationName . '($relationAlias = null) Adds a RIGHT JOIN clause to the query using the ' . $relationName . " relation
* @method $queryClass innerJoin" . $relationName . '($relationAlias = null) Adds a INNER JOIN clause to the query using the ' . $relationName . " relation
*";

$script .= "
* @method $queryClass joinWith" . $relationName . '($joinType = Criteria::INNER_JOIN) Adds a join clause and with to the query using the ' . $relationName . " relation
*";
$vars = [
'tableName' => $table->getName(),
'tableDesc' => $table->getDescription(),
'queryClass' => $this->getQueryClassName(),
'modelClass' => $this->getObjectClassName(),
'parentClass' => $this->getParentClass(),
'entityNotFoundExceptionClass' => $this->getEntityNotFoundExceptionClass(),
'unqualifiedClassName' => $this->getUnqualifiedClassName(),

$script .= "
* @method $queryClass leftJoinWith" . $relationName . '() Adds a LEFT JOIN clause and with to the query using the ' . $relationName . " relation
* @method $queryClass rightJoinWith" . $relationName . '() Adds a RIGHT JOIN clause and with to the query using the ' . $relationName . " relation
* @method $queryClass innerJoinWith" . $relationName . '() Adds a INNER JOIN clause and with to the query using the ' . $relationName . " relation
*";
'addTimestamp' => $this->getBuildProperty('generator.objectModel.addTimeStamp'),
'propelVersion' => $this->getBuildProperty('general.version'),

$relationQueryClasses[$this->getNewStubQueryBuilder($refFK->getTable())->getQueryClassName(true)] = true;
}
'columns' => $table->getColumns(),

if ($relationQueryClasses) {
$relationQueryClasses = implode('|', array_keys($relationQueryClasses));
$script .= "
* @method $relationQueryClasses endUse() Finalizes a secondary criteria and merges it with its primary Criteria
*";
}
'relationNames' => $this->getRelationNames(),
'relatedTableQueryClassNames' => $this->getRelatedTableQueryClassNames(),
];

// override the signature of ModelCriteria::findOne() to specify the class of the returned object, for IDE completion
$script .= "
* @method $modelClass|null findOne(?ConnectionInterface \$con = null) Return the first $modelClass matching the query
* @method $modelClass findOneOrCreate(?ConnectionInterface \$con = null) Return the first $modelClass matching the query, or a new $modelClass object populated from the query conditions when no match is found
*";
$templatePath = $this->getTemplatePath(__DIR__);

// magic findBy() methods, for IDE completion
foreach ($this->getTable()->getColumns() as $column) {
$script .= "
* @method $modelClass|null findOneBy" . $column->getPhpName() . '(' . $column->getPhpType() . ' $' . $column->getName() . ") Return the first $modelClass filtered by the " . $column->getName() . ' column';
}
$template = new PropelTemplate();
$filePath = $templatePath . 'baseQueryClassHeader.php';
$template->setTemplateFile($filePath);

$script .= " * \n";
$script .= $template->render($vars);
}

// override the signature of ModelCriteria::require*() to specify the class of the returned object, for IDE completion
$script .= "
* @method $modelClass requirePk(\$key, ?ConnectionInterface \$con = null) Return the $modelClass by primary key and throws {$this->getEntityNotFoundExceptionClass()} when not found
* @method $modelClass requireOne(?ConnectionInterface \$con = null) Return the first $modelClass matching the query and throws {$this->getEntityNotFoundExceptionClass()} when not found
*";
/**
* Get names of all foreign key relations to and from this table.
*
* @return array<string>
*/
protected function getRelationNames(): array
{
$table = $this->getTable();
$fkRelationNames = array_map([$this, 'getFKPhpNameAffix'], $table->getForeignKeys());
$refFkRelationNames = array_filter(array_map([$this, 'getRefFKPhpNameAffix'], $table->getReferrers()));

// magic requireOneBy() methods, for IDE completion
foreach ($this->getTable()->getColumns() as $column) {
$script .= "
* @method $modelClass requireOneBy" . $column->getPhpName() . '(' . $column->getPhpType() . ' $' . $column->getName() . ") Return the first $modelClass filtered by the " . $column->getName() . " column and throws {$this->getEntityNotFoundExceptionClass()} when not found";
}
return array_merge($fkRelationNames, $refFkRelationNames);
}

$script .= "
*
* @method {$modelClass}[]|Collection find(?ConnectionInterface \$con = null) Return $modelClass objects based on current ModelCriteria
* @psalm-method Collection&\Traversable<{$modelClass}> find(?ConnectionInterface \$con = null) Return $modelClass objects based on current ModelCriteria";
foreach ($this->getTable()->getColumns() as $column) {
$script .= "
* @method {$modelClass}[]|Collection findBy" . $column->getPhpName() . '(' . $column->getPhpType() . ' $' . $column->getName() . ") Return $modelClass objects filtered by the " . $column->getName() . ' column' . "
* @psalm-method Collection&\Traversable<{$modelClass}> findBy" . $column->getPhpName() . '(' . $column->getPhpType() . ' $' . $column->getName() . ") Return $modelClass objects filtered by the " . $column->getName() . ' column';
}
/**
* Get query class names of all tables connected to this table with a foreign key relation.
*
* @return array<string>
*/
protected function getRelatedTableQueryClassNames(): array
{
$table = $this->getTable();
$fkTables = array_map(fn ($fk) => $fk->getForeignTable(), $table->getForeignKeys());
$refFkTables = array_map(fn ($fk) => $fk->getTable(), $table->getReferrers());
$relationTables = array_merge($fkTables, $refFkTables);

$script .= "
* @method {$modelClass}[]|\\Propel\\Runtime\\Util\\PropelModelPager paginate(\$page = 1, \$maxPerPage = 10, ?ConnectionInterface \$con = null) Issue a SELECT query based on the current ModelCriteria and uses a page and a maximum number of results per page to compute an offset and a limit
* @psalm-method \\Propel\\Runtime\\Util\\PropelModelPager&\Traversable<{$modelClass}> paginate(\$page = 1, \$maxPerPage = 10, ?ConnectionInterface \$con = null) Issue a SELECT query based on the current ModelCriteria and uses a page and a maximum number of results per page to compute an offset and a limit
*
*/
abstract class " . $this->getUnqualifiedClassName() . ' extends ' . $parentClass . "
{
";
return array_map(fn ($table) => $this->getNewStubQueryBuilder($table)->getQueryClassName(true), $relationTables);
}

/**
Expand Down Expand Up @@ -327,7 +238,7 @@ protected function addClassBody(string &$script): void
*/
protected function addEntityNotFoundExceptionClass(string &$script): void
{
$script .= "protected \$entityNotFoundExceptionClass = '" . addslashes($this->getEntityNotFoundExceptionClass()) . "';\n";
$script .= " protected \$entityNotFoundExceptionClass = '" . addslashes($this->getEntityNotFoundExceptionClass()) . "';\n";
}

/**
Expand Down
92 changes: 92 additions & 0 deletions templates/Builder/Om/baseQueryClassHeader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/*
* vars:
* - string $tableName
* - string $tableDesc
* - string $queryClass
* - string $modelClass
* - string $parentClass
* - string $entityNotFoundExceptionClass
* - string $unqualifiedClassName
*
* - bool $addTimestamp
* - string $propelVersion
*
* - Column[] $columns
* - string[] $relationNames (both ref and backref)
* - string[] $relatedTableQueryClassNames
*/
?>

/**
* Base class that represents a query for the <?= $tableName ?> table.
*
<?php if ($tableDesc): ?>
* <?= $tableDesc ?>
*
<?php endif; ?>
<?php if ($addTimestamp): ?>
* This class was autogenerated by Propel <?= $propelVersion ?> on:
*
* <?= strftime('%c') ?>
*
<?php endif; ?>
<?php foreach($columns as $column):?>
* @method <?= $queryClass ?> orderBy<?= $column->getPhpName() ?>($order = Criteria::ASC) Order by the <?= $column->getName() ?> column
<?php endforeach;?>
*
<?php foreach($columns as $column):?>
* @method <?= $queryClass ?> groupBy<?= $column->getPhpName() ?>() Group by the <?= $column->getName() ?> column';
<?php endforeach;?>
*
* @method <?= $queryClass ?> leftJoin($relation) Adds a LEFT JOIN clause to the query
* @method <?= $queryClass ?> rightJoin($relation) Adds a RIGHT JOIN clause to the query
* @method <?= $queryClass ?> innerJoin($relation) Adds a INNER JOIN clause to the query
*
* @method <?= $queryClass ?> leftJoinWith($relation) Adds a LEFT JOIN clause and with to the query
* @method <?= $queryClass ?> rightJoinWith($relation) Adds a RIGHT JOIN clause and with to the query
* @method <?= $queryClass ?> innerJoinWith($relation) Adds a INNER JOIN clause and with to the query
*
<?php foreach($relationNames as $relationName):?>
* @method <?= $queryClass ?> leftJoin<?= $relationName ?>($relationAlias = null) Adds a LEFT JOIN clause to the query using the <?= $relationName ?> relation
* @method <?= $queryClass ?> rightJoin<?= $relationName ?>($relationAlias = null) Adds a RIGHT JOIN clause to the query using the <?= $relationName ?> relation
* @method <?= $queryClass ?> innerJoin<?= $relationName ?>($relationAlias = null) Adds a INNER JOIN clause to the query using the <?= $relationName ?> relation
*
* @method <?= $queryClass ?> joinWith<?= $relationName ?>($joinType = Criteria::INNER_JOIN) Adds a join clause and with to the query using the ' . $relationName . " relation
*
* @method <?= $queryClass ?> leftJoinWith<?= $relationName ?>() Adds a LEFT JOIN clause and with to the query using the <?= $relationName ?> relation
* @method <?= $queryClass ?> rightJoinWith<?= $relationName ?>() Adds a RIGHT JOIN clause and with to the query using the <?= $relationName ?> relation
* @method <?= $queryClass ?> innerJoinWith<?= $relationName ?>() Adds a INNER JOIN clause and with to the query using the <?= $relationName ?> relation
*
<?php endforeach; ?>
<?php if($relatedTableQueryClassNames): ?>
* @method <?= implode('|', $relatedTableQueryClassNames) ?> endUse() Finalizes a secondary criteria and merges it with its primary Criteria
*
<?php endif; ?>
* @method <?= $modelClass ?>|null findOne(?ConnectionInterface $con = null) Return the first <?= $modelClass ?> matching the query
* @method <?= $modelClass ?> findOneOrCreate(?ConnectionInterface $con = null) Return the first <?= $modelClass ?> matching the query, or a new <?= $modelClass ?> object populated from the query conditions when no match is found
*
<?php foreach($columns as $column):?>
* @method <?= $modelClass ?>|null findOneBy<?= $column->getPhpName() ?>(<?= $column->getPhpType() ?> $<?= $column->getName() ?>) Return the first <?= $modelClass ?> filtered by the <?= $column->getName() ?> column
<?php endforeach;?>
*
* @method <?= $modelClass ?> requirePk($key, ?ConnectionInterface $con = null) Return the <?= $modelClass ?> by primary key and throws <?= $entityNotFoundExceptionClass ?> when not found
* @method <?= $modelClass ?> requireOne(?ConnectionInterface $con = null) Return the first <?= $modelClass ?> matching the query and throws <?= $entityNotFoundExceptionClass ?> when not found
*
<?php foreach($columns as $column):?>
* @method <?= $modelClass ?> requireOneBy<?= $column->getPhpName() ?>(<?= $column->getPhpType() ?> $<?= $column->getName() ?>) Return the first <?= $modelClass ?> filtered by the $name column and throws <?= $entityNotFoundExceptionClass ?> when not found
<?php endforeach;?>
*
* @method <?= $modelClass ?>[]|Collection find(?ConnectionInterface $con = null) Return <?= $modelClass ?> objects based on current ModelCriteria
* @psalm-method Collection&\Traversable<<?= $modelClass ?>> find(?ConnectionInterface $con = null) Return <?= $modelClass ?> objects based on current ModelCriteria
*
<?php foreach($columns as $column):?>
* @method <?= $modelClass ?>[]|Collection findBy<?= $column->getPhpName() ?>(<?= $column->getPhpType() ?>|array<<?= $column->getPhpType() ?>> $<?= $column->getName() ?>) Return <?= $modelClass ?> objects filtered by the <?= $column->getName() ?> column
* @psalm-method Collection&\Traversable<<?= $modelClass ?>> findBy<?= $column->getPhpName() ?>(<?= $column->getPhpType() ?>|array<<?= $column->getPhpType() ?>> $<?= $column->getName() ?>) Return <?= $modelClass ?> objects filtered by the <?= $column->getName() ?> column
<?php endforeach;?>
*
* @method <?= $modelClass ?>[]|\Propel\Runtime\Util\PropelModelPager paginate($page = 1, $maxPerPage = 10, ?ConnectionInterface $con = null) Issue a SELECT query based on the current ModelCriteria and uses a page and a maximum number of results per page to compute an offset and a limit
* @psalm-method \Propel\Runtime\Util\PropelModelPager&\Traversable<<?= $modelClass ?>> paginate($page = 1, $maxPerPage = 10, ?ConnectionInterface $con = null) Issue a SELECT query based on the current ModelCriteria and uses a page and a maximum number of results per page to compute an offset and a limit
*/
abstract class <?= $unqualifiedClassName ?> extends <?= $parentClass ?>
{

0 comments on commit cb84d9c

Please sign in to comment.