Skip to content

Commit

Permalink
Merge pull request #1670 from nederdirk/withQuery-function
Browse files Browse the repository at this point in the history
Add `withXyzQuery` function to aid static type checking
  • Loading branch information
dereuromark authored Feb 9, 2021
2 parents 79ed168 + 8ae1f01 commit 394a6ce
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/Propel/Generator/Builder/Om/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1549,6 +1549,7 @@ protected function addUseFkQuery(&$script, $fk)
$joinType = $this->getJoinType($fk);

$this->addUseRelatedQuery($script, $fkTable, $queryClass, $relationName, $joinType);
$this->addWithRelatedQuery($script, $fkTable, $queryClass, $relationName, $joinType);
}

/**
Expand All @@ -1568,6 +1569,7 @@ protected function addUseRefFkQuery(&$script, ForeignKey $fk)
$joinType = $this->getJoinType($fk);

$this->addUseRelatedQuery($script, $fkTable, $queryClass, $relationName, $joinType);
$this->addWithRelatedQuery($script, $fkTable, $queryClass, $relationName, $joinType);
}

/**
Expand Down Expand Up @@ -1604,6 +1606,48 @@ public function use" . $relationName . 'Query($relationAlias = null, $joinType =
";
}

/**
* Adds a withRelatedQuery method for this object.
*
* @param string $script The script will be modified in this method.
* @param \Propel\Generator\Model\Table $fkTable
* @param string $queryClass
* @param string $relationName
* @param string $joinType
*
* @return void
*/
protected function addWithRelatedQuery(&$script, Table $fkTable, $queryClass, $relationName, $joinType)
{
$script .= "
/**
* Use the {$relationName} relation {$fkTable->getPhpName()} object
*
* @param callable({$queryClass}):{$queryClass} \$callable A function working on the related query
*
* @param string|null \$relationAlias optional alias for the relation
*
* @param string|null \$joinType Accepted values are null, 'left join', 'right join', 'inner join'
*
* @return \$this
*/
public function with{$relationName}Query(
callable \$callable,
string \$relationAlias = null,
?string \$joinType = {$joinType}
) {
\$relatedQuery = \$this->use{$relationName}Query(
\$relationAlias,
\$joinType
);
\$callable(\$relatedQuery);
\$relatedQuery->endUse();
return \$this;
}
";
}

/**
* @param string $script
* @param \Propel\Generator\Model\CrossForeignKeys $crossFKs
Expand Down
30 changes: 30 additions & 0 deletions tests/Propel/Tests/Generator/Builder/Om/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,36 @@ public function testUseFkQuerySimple()
$this->assertTrue($q->equals($q1), 'useFkQuery() translates to a condition on an inner join on required columns');
}

/**
* @return void
*/
public function testUseFkQueryWith()
{
$q = BookQuery::create()
->withAuthorQuery(
function (AuthorQuery $q) {
return $q->filterByFirstName('Leo');
}
);
$q1 = BookQuery::create()
->useAuthorQuery()
->filterByFirstName('Leo')
->endUse();
$this->assertTrue($q->equals($q1), 'useFkQuery() translates to a condition on a left join on non-required columns');

$q = BookSummaryQuery::create()
->withSummarizedBookQuery(
function (BookQuery $q) {
return $q->filterByTitle('War and Peace');
}
);
$q1 = BookSummaryQuery::create()
->useSummarizedBookQuery()
->filterByTitle('War and Peace')
->endUse();
$this->assertEquals($q1, $q, 'useFkQuery() translates to a condition on an inner join on required columns');
}

/**
* @return void
*/
Expand Down

0 comments on commit 394a6ce

Please sign in to comment.