Last active
August 29, 2015 14:05
-
-
Save mrimann/79fe0ec2902d51003721 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
There are Installations with 0..n components attached. The following query should | |
return matching Installations that have e.g. a component like e.g. "tt_news with version X". | |
But it returns any installation that has any version of "tt_news" as component, and | |
also any Installation containing *any* component with version "X" | |
*/ | |
class InstallationRepository extends \TYPO3\Flow\Persistence\Repository { | |
/** | |
* Searches for installations that match to a bunch of parameters. | |
* | |
* @param string $productKey | |
* @return \TYPO3\Flow\Persistence\QueryResultInterface | |
*/ | |
public function findBySearchQuery($productKey, $components) { | |
$query = $this->createQuery(); | |
$query->setOrderings( | |
array( | |
'url' => \TYPO3\Flow\Persistence\QueryInterface::ORDER_ASCENDING | |
) | |
); | |
$componentQueryParts = array(); | |
if (count($components)) { | |
foreach ($components as $component) { | |
if (isset($component['version'])) { | |
// TODO: This queries *any* component individually, but not as intended like "component X in version Y", damned! | |
$singleComponentQuery = $query->logicalAnd( | |
$query->equals('components.keyComponent', $component['key']), | |
$query->like('components.version', $component['version']) | |
); | |
} else { | |
$singleComponentQuery = $query->equals('components.keyComponent', $component['key']); | |
} | |
$componentQueryParts[] = $singleComponentQuery; | |
} | |
} | |
$query->matching( | |
$query->logicalAnd( | |
$query->equals('product', $productKey), | |
$query->logicalAnd( | |
$componentQueryParts | |
) | |
) | |
); | |
return $query->execute(); | |
} |
Ok, I could get it to work and my example code looks like this for the moment:
$results = $this->entityManager->createQuery(
'SELECT i FROM \Internezzo\VersionControl\Domain\Model\Installation i
JOIN \Internezzo\VersionControl\Domain\Model\Component c WITH i = c.installation
WHERE i.product=:product
AND c.keyComponent=:componentKey
AND c.version LIKE :componentVersion1
ORDER BY i.url'
)
->setParameter('componentKey', 'extbase')
->setParameter('componentVersion1', '1.1.%')
->setParameter('product', $productKey)
->execute();
return $results;
Of course the hard-coded parameters here (componentKey and componentVersion) in this example will be stringed together dynamically depending on the input. Thanks Kay for your input!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
USE DQL ... it's easier to read then the create query stuff ...
more documentation is in the doctrine docs ...