This repository has been archived by the owner on Nov 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
/** | ||
* This file is part of the Nella Framework. | ||
* | ||
* Copyright (c) 2006, 2011 Patrik Votoček (http://patrik.votocek.cz) | ||
* | ||
* This source file is subject to the GNU Lesser General Public License. For more information please see http://nella-project.org | ||
*/ | ||
|
||
namespace Nella\Doctrine; | ||
|
||
use DoctrineExtensions\Paginate\Paginate, | ||
Nella\Model\IQueryable; | ||
|
||
/** | ||
* Query Object | ||
* | ||
* @author Patrik Votoček | ||
* | ||
* @property-read \Nette\Utils\Paginator|NULL $paginator | ||
*/ | ||
class QueryObject extends \Nette\Object implements \Nella\Model\IQueryObject | ||
{ | ||
/** @var \Nette\Utils\Paginator */ | ||
private $paginator; | ||
|
||
/** | ||
* @param \Nette\Utils\Paginator | ||
*/ | ||
public function __construct(\Nette\Utils\Paginator $paginator = NULL) | ||
{ | ||
$this->paginator = $paginator; | ||
} | ||
|
||
/** | ||
* @return \Nette\Utils\Paginator|NULL | ||
*/ | ||
public function getPaginator() | ||
{ | ||
return $this->paginator; | ||
} | ||
|
||
/** | ||
* @param \Nella\Model\IQueryable | ||
* @return \Doctrine\ORM\Query|Doctrine\CouchDB\View\AbstractQuery | ||
*/ | ||
protected function doCreateQuery(IQueryable $broker) | ||
{ | ||
return $broker->createQueryBuilder('qo')->getQuery(); | ||
} | ||
|
||
/** | ||
* @param IQueryable | ||
* @return int | ||
*/ | ||
public function count(IQueryable $broker) | ||
{ | ||
return Paginate::getTotalQueryResults($this->doCreateQuery($broker)); | ||
} | ||
|
||
/** | ||
* @param \Nella\Model\IQueryable | ||
* @return \Doctrine\Common\Collections\Collection|array | ||
*/ | ||
public function fetch(IQueryable $broker) | ||
{ | ||
$query = $this->doCreateQuery($broker); | ||
|
||
if ($this->paginator) { // Paginate | ||
$query = Paginate::getPaginateQuery($query, $this->paginator->getOffset(), $this->paginator->getLength()); | ||
} | ||
|
||
try{ | ||
return $query->getResult(); | ||
} catch (\Doctrine\ORM\NoResultException $e) { | ||
return array(); | ||
} | ||
} | ||
|
||
/** | ||
* @param \Nella\Model\IQueryable | ||
* @return \Nella\Doctrine\Entity|object|NULL | ||
*/ | ||
public function fetchOne(IQueryable $broker) | ||
{ | ||
$query = $this->doCreateQuery($broker); | ||
|
||
$query->setMaxResults(1); | ||
|
||
try{ | ||
return $query->getSingleResult(); | ||
} catch (\Doctrine\ORM\NoResultException $e) { | ||
return NULL; | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
/** | ||
* This file is part of the Nella Framework. | ||
* | ||
* Copyright (c) 2006, 2011 Patrik Votoček (http://patrik.votocek.cz) | ||
* | ||
* This source file is subject to the GNU Lesser General Public License. For more information please see http://nellacms.com | ||
*/ | ||
|
||
namespace NellaTests\Doctrine; | ||
|
||
use Nella\Doctrine\QueryObject; | ||
|
||
class QueryObjectTest extends \Nella\Testing\TestCase | ||
{ | ||
/** @var \Nella\Doctrine\IQueryObject */ | ||
private $query; | ||
|
||
public function setup() | ||
{ | ||
parent::setup(); | ||
$this->query = new QueryObject; | ||
} | ||
|
||
public function testInstance() | ||
{ | ||
$this->assertInstanceOf('Nella\Model\IQueryObject', $this->query, 'is instance "QueryObject"'); | ||
} | ||
|
||
public function testGetPaginator() | ||
{ | ||
$this->assertNull($this->query->getPaginator(), '->getPaginator()'); | ||
$this->assertNull($this->query->paginator, '->paginator'); | ||
|
||
$paginator = new \Nette\Utils\Paginator; | ||
$query = new QueryObject($paginator); | ||
$this->assertSame($paginator, $query->getPaginator(), '->getPaginator() same'); | ||
} | ||
} |
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