Skip to content

Instantly share code, notes, and snippets.

@matej21
Created September 1, 2014 21:55
Show Gist options
  • Save matej21/847008eaa23378703a79 to your computer and use it in GitHub Desktop.
Save matej21/847008eaa23378703a79 to your computer and use it in GitHub Desktop.
Kdyby\Doctrine
<?php
$dao = $em->getDao(Article::class);
$allArticles = $dao->findAll();
$filteredAndSorted = $dao->findBy(['category' => $category], ['title' => 'asc']);
$singleByFilter = $dao->findOneBy(['title' => 'Foo bar']);
$single = $dao->find(1);
<?php
class ArticleQuery extends \Kdyby\Doctrine\QueryObject
{
protected $category;
public function setCategory(Category $category)
{
$this->category = $category;
}
protected function doCreateQuery(Kdyby\Persistence\Queryable $repository)
{
$qb = $repository->createQueryBuilder('article');
if($this->category) {
$qb->andWhere('article.category = :category')
->setParameter('category', $this->category);
}
return $qb;
}
}
$dao = $em->getDao(Article::class);
$query = new ArticleQuery();
$query->setCategory($category);
$result = $dao->fetch($query);
$paginator = $this['visualPaginator']->getPaginator();
$result->applyPaginator($paginator);
foreach($result as $article) {
echo $article->title;
}
}
<?php
$article = new Article;
$article->setTitle('foo bar');
$em->persist($article); //query is not executed
//sth else
$em->flush(); //all queries are executed in single transaction
$article->setTile('xxx'); //title changed
$em->flush(); //just flush, we don't have to persist it again
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment