Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Doctrine: add basic QueryObject

  • Loading branch information...
commit 8b9a25e13493be06e637986b900be29b2b05535a 1 parent 3b9d579
@Vrtak-CZ Vrtak-CZ authored
View
96 Nella/Doctrine/QueryObject.php
@@ -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;
+ }
+ }
+}
View
4 README.md
@@ -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
------------
View
39 tests/cases/Doctrine/QueryObjectTest.php
@@ -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');
+ }
+}
View
12 vendors.sh
@@ -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"
##################
@@ -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"
###########
@@ -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"
Please sign in to comment.
Something went wrong with that request. Please try again.