Skip to content

Instantly share code, notes, and snippets.

@mrimann
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrimann/79fe0ec2902d51003721 to your computer and use it in GitHub Desktop.
Save mrimann/79fe0ec2902d51003721 to your computer and use it in GitHub Desktop.
<?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();
}
@kaystrobach
Copy link

USE DQL ... it's easier to read then the create query stuff ...

    /**
     * @param Schulart $schulart
     * @return mixed
     */
    public function findFachBySchulart(Schulart $schulart) {
        return $this->entityManager
            ->createQuery(
                'SELECT f FROM \SBS\LaPo\Domain\Model\Fach f JOIN \SBS\LaPo\Domain\Model\Fachbereich b WITH f = b.fach WHERE b.schulart=:schulart ORDER BY f.fachbezeichnung'
            )
            ->setParameter('schulart', $schulart)
            ->execute();
    }

more documentation is in the doctrine docs ...

@mrimann
Copy link
Author

mrimann commented Aug 29, 2014

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