-
-
Save bwaidelich/5433673 to your computer and use it in GitHub Desktop.
<?php | |
namespace Your\Package\Domain\Repository; | |
use TYPO3\Flow\Annotations as Flow; | |
use TYPO3\Flow\Persistence\Doctrine\Repository; | |
use TYPO3\Flow\Reflection\ObjectAccess; | |
/** | |
* @Flow\Scope("singleton") | |
*/ | |
class ProductRepository extends Repository { | |
/** | |
* @param string $foo | |
* @return \TYPO3\Flow\Persistence\QueryResultInterface | |
*/ | |
public function findByFoo($foo) { | |
$query = $this->createQuery(); | |
/** @var $queryBuilder \Doctrine\ORM\QueryBuilder **/ | |
// TODO !hack alarm! replace this once API is available in TYPO3.Flow: | |
$queryBuilder = ObjectAccess::getProperty($query, 'queryBuilder', TRUE); | |
$queryBuilder | |
->resetDQLParts() | |
->select('product') | |
->from('Your\Package\Domain\Model\Product', 'product') | |
->where('product.active = 1'); | |
->andWhere('product.foo = :foo') | |
->orderBy('product.title') | |
->addOrderBy('product.price', 'DESC') | |
->setParameter('foo', $foo) | |
return $query->execute(); | |
} | |
} | |
?> |
Hey Basti, nice trick. We should find some way too support DQL directly though. Was wondering if 1. is really necessary? Doesn't the generic repository use a Doctrine querybuilder anyway? Unless you use some other persistence but then DQL wont work anyway.
Julle, just see your comment now ;)
You're right, it should work with the generic base repository, too.
BTW: Instead of using the fluent interface, you can also set raw DQL or use $queryBuilder->add()
Is there an update on how to use DQL with Flow? Do we still have to use the ObjectAccess::getProperty() hack or is there some API in the meantime?
Is there an update on how to use DQL with Flow? Do we still have to use the ObjectAccess::getProperty() hack or is there some API in the meantime?
http://stackoverflow.com/questions/27443604/get-table-name-of-an-entity-for-a-query-in-typo3-flow
See the first answer.
Edit:
protected $entityManager;
$dql = 'SELECT COUNT(e) FROM Vendor\Package\Domain\Model\Entity e WHERE e.property = :property';
$query = $this->entityManager->createQuery($dql);
$query->setParameters(array('property' => $property));
$result = $query->execute();
DISCLAIMER: This currently requires a hack to access the QueryBuilder from Query. We're working on making that accessible via the API asap and when that's the case you should replace the code in question (line 21 in this example).
Notes: