Skip to content
This repository has been archived by the owner on Nov 15, 2020. It is now read-only.

Commit

Permalink
Doctrine: add basic QueryObject
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrtak-CZ committed Mar 14, 2012
1 parent 3b9d579 commit 8b9a25e
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 0 deletions.
96 changes: 96 additions & 0 deletions Nella/Doctrine/QueryObject.php
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;
}
}
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Nella Framework requires PHP 5.3.2 with PDO.
- Doctrine ORM 2.2.x
- Symfony Console 2.0.x

Optional

- Doctrine Extension - Paginate


Installation
------------
Expand Down
39 changes: 39 additions & 0 deletions tests/cases/Doctrine/QueryObjectTest.php
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');
}
}
12 changes: 12 additions & 0 deletions vendors.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
VENDOR_VERSION_NETTE="2.0.1"
VENDOR_VERSION_DOCTRINE="2.2.1"
VENDOR_VERSION_DOCTRINE_MIGRATIONS="master"
VENDOR_VERSION_DOCTRINE_EXTENSIONS="master"
VENDOR_VERSION_SYMFONY_CONSOLE="2.0.11"

VENDOR_URL_NETTE="http://files.nette.org/releases/2.0/NetteFramework-$VENDOR_VERSION_NETTE-PHP5.3.tar.bz2"
VENDOR_URL_DOCTRINE="http://www.doctrine-project.org/downloads/DoctrineORM-$VENDOR_VERSION_DOCTRINE-full.tar.gz"
VENDOR_URL_DOCTRINE_MIGRATIONS="https://github.com/doctrine/migrations/tarball/$VENDOR_VERSION_DOCTRINE_MIGRATIONS"
VENDOR_URL_DOCTRINE_EXTENSIONS="https://github.com/beberlei/DoctrineExtensions/tarball/$VENDOR_VERSION_DOCTRINE_EXTENSIONS"
VENDOR_URL_SYMFONY_CONSOLE="http://pear.symfony.com/get/Console-$VENDOR_VERSION_SYMFONY_CONSOLE.tgz"

##################
Expand All @@ -28,6 +30,7 @@ mkdir "vendors"
wget --no-check-certificate $VENDOR_URL_NETTE -O "vendors/_Nette.tar.bz2"
wget --no-check-certificate $VENDOR_URL_DOCTRINE -O "vendors/_Doctrine.tar.gz"
wget --no-check-certificate $VENDOR_URL_DOCTRINE_MIGRATIONS -O "vendors/_DoctrineMigrations.tar.gz"
wget --no-check-certificate $VENDOR_URL_DOCTRINE_EXTENSIONS -O "vendors/_DoctrineExtensions.tar.gz"
wget --no-check-certificate $VENDOR_URL_SYMFONY_CONSOLE -O "vendors/_SymfonyConsole.tar.gz"

###########
Expand Down Expand Up @@ -62,6 +65,15 @@ mv "vendors/_DoctrineMigrations/$TMP_MIGRATIONS/lib/Doctrine/DBAL/Migrations" "v
mv "vendors/_DoctrineMigrations/$TMP_MIGRATIONS/LICENSE" "vendors/Doctrine/license-migrations.txt"
rm -rf "vendors/_DoctrineMigrations"

# Doctrine Extensions
mkdir "vendors/_DoctrineExtensions"
tar xzvf "vendors/_DoctrineExtensions.tar.gz" -C "vendors/_DoctrineExtensions"
rm "vendors/_DoctrineExtensions.tar.gz"
TMP_EXTENSIONS=`ls "vendors/_DoctrineExtensions" | grep DoctrineExtensions`
mkdir 'vendors/DoctrineExtensions'
mv "vendors/_DoctrineExtensions/$TMP_EXTENSIONS/lib/DoctrineExtensions/Paginate" "vendors/DoctrineExtensions/Paginate"
rm -rf "vendors/_DoctrineExtensions"

# Symfony Console
mkdir "vendors/_SymfonyConsole"
tar xzvf "vendors/_SymfonyConsole.tar.gz" -C "vendors/_SymfonyConsole"
Expand Down

0 comments on commit 8b9a25e

Please sign in to comment.