Skip to content

Instantly share code, notes, and snippets.

@bwaidelich
Last active December 16, 2015 12:19
Show Gist options
  • Select an option

  • Save bwaidelich/5433673 to your computer and use it in GitHub Desktop.

Select an option

Save bwaidelich/5433673 to your computer and use it in GitHub Desktop.
This snippet shows you how you can use Fluid widgets with DQL queries in TYPO3.Flow today with a little hack
<?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();
}
}
?>
@bwaidelich
Copy link
Author

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()

@christian-fries
Copy link

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?

@itskevinsam
Copy link

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:

  • @flow\Inject
  • @var \Doctrine\Common\Persistence\ObjectManager

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();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment