Created
September 1, 2014 21:55
-
-
Save matej21/847008eaa23378703a79 to your computer and use it in GitHub Desktop.
Kdyby\Doctrine
This file contains 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 | |
$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); | |
This file contains 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 | |
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; | |
} | |
} | |
This file contains 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 | |
$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